Array Concepts
An array is a set of elements of the same data type arranged in a certain order, that is, a variable of the same type is named with a name, and then the set of the variable is distinguished by a number, which is called the array name, and the number is called the subscript. Each variable that makes up an array is called the component of an array, also known as an element of an array, sometimes called a subscript variable. An array is a form in which a number of variables of the same type are organized in an orderly manner in order to deal with them conveniently. The collection of these ordered, homogeneous data elements is called an array.
Arrays allow us to use a simple set in the process of program development, the so-called simple is in the same array set of all elements must be the same type, different development languages have the concept of arrays, using arrays we can use a class of variables to be collected together.
In Java, there are one-dimensional arrays and multidimensional arrays of the array structure, one-dimensional arrays are often used by us, the following from the perspective of the use of the array, the following is an example of an array.
Two ways to initialize a Java array:
Static initialization: The programmer assigns values to each element of an array when initializing it;
Dynamic initialization: When an array is initialized, the programmer only specifies the length of the array, and the system assigns an initial value to each element.
- Public class Arrayinit {
- public static void Main (string[] args) {
- //Static initialization array: Method one
- String cats[] = new string[] {
- "Tom","Sam", "Mimi"
- };
- //Static initialization array: Method two
- String dogs[] = {"Jimmy","Gougou","Doggy"};
- //dynamic initialization of data
- String books[] = new string[2];
- books[0] = "thinking in Java";
- books[1] = "Effective Java";
- System.out.println (cats.length);
- System.out.println (dogs.length);
- System.out.println (books.length);
- }
- }
A Java array is a reference data type. An array variable is not an array itself, but a pointer to an array object stored in the heap memory. Therefore, you can change the array referenced by an array variable. Look at the code below.
- Public class Arrayinit {
- public static void Main (string[] args) {
- //Static initialization array: Method one
- String cats[] = new string[] {
- "Tom","Sam", "Mimi"
- };
- //Static initialization array: Method two
- String dogs[] = {"Jimmy","Gougou","Doggy"};
- //dynamic initialization of data
- String books[] = new string[2];
- books[0] = "thinking in Java";
- books[1] = "Effective Java";
- Cats = dogs;
- Books = dogs;
- PrintArray (cats);
- PrintArray (dogs);
- PrintArray (books);
- }
- private static void PrintArray (string[] arr) {
- For (int i=0; i < arr.length; i++) {
- System.out.println (Arr[i]);
- }
- }
- }
2. Whether an array must be initialized
The key to this problem is to figure out the difference between array variables and arrays of objects. The array variable is stored in the stack memory, and the array object is stored in the heap memory. An array variable is simply a reference variable that can point to the actual array object.
The so-called array initialization is not the initialization of a logarithmic set of variables, but the initialization of arrays of objects.
Dark Horse programmer ———————— java array