Array of Java Beginners

What is an array? An array is a variable used to store data of the same data type in memory. Each data element in the array belongs to a data type.

Basic elements of an array:

           Identifier:

                       Arrays have only one name, an identifier. Used to distinguish different arrays.

           Array elements:

                        When the name of the array is given, that is, the array identifier, to store data in the array, these data are called array elements

           Array subscript:

                        In an array, in order to get the elements of the array correctly, they need to be numbered, so that the computer can access according to the number, this number is called the array subscript.

           Element Type:

                         Array elements stored in an array should be of the same data type.

Array out of bounds: When defining an array, it also defines the length of the array. If the array is full and you want to store data in the array, the program will fail, which is called array out of bounds.

 Steps to use arrays:

                         1. Declare the array

                                Syntax for declaring an array:

                                               data type[ ] array name;//or data type array name[ ];

                         2. Allocate space

                                               Allocating space is telling the computer to allocate some contiguous space in memory to store data. In java, you can use the new keyword to allocate space to an array.

                                  Syntax for allocating space:

                                                array name=new data type[array length];

                                  Allocating space while declaring an array: syntax

                                                datatype[] arrayname = new datatype[array length];

                         3. Assignment

                                   datatype[] arrayname={value1, value2........};

                                  Example:

                                    int[]  scores={60,70,90,80,76};

                                Equivalent to    int[] scores=int[] {60,70,90,80,76};

Common mistakes:

                 1. Array subscripts start from 0;

                 2. Array access is out of bounds;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326844834&siteId=291194637