JAVA one-dimensional array object, array declaration initialization, NullPointerException, null pointer exception, Arrays class method and four shallow copies

0. Get familiar with one-dimensional array  

Array: a collection (container) of elements of the same data type. An array is a contiguous space that stores the same data type in memory. Memory is continuous.

For example, if you declare array a, on array a,   You can access his properties or call some of his methods. Basically, it can be concluded that arrays in java are also objects. They have some basic characteristics of other objects in java: they encapsulate some data, access properties, and call methods. Therefore, arrays are objects.



To declare an array is to delimit a series of contiguous spaces in memory space. The difference between this and C language  

In c language, the pointer stores the address of the array. The address of the first element of the array is the address of the array,
The array name represents the first address of a contiguous space
Through the first address, all elements of the array can be accessed in turn
The ordering of elements in an array is called subscript starting from zero

The content elements from new new in the array are stored in Heap. The variable array name applies for memory on the stack. Array objects exist on the Heap.

 

Array name: variable name. The address of variable name (array name) is stored on the stack. As an object-oriented language, it has been object-oriented since the array

His int num={1,2,3,4,5}; The right side of the medium belongs to the object,

1. Declaration method of Java one-dimensional array:

1.1. Static initialization

		int[] array= {0,1,2,3};
		int[] array0=new int[] {1,2,3,4};//Specify the element, JAVA will recognize the array length, static initialization,

For the above two, although the first declaration method does not have new, it is better to be concise and convenient. No new here is better than new;

Second, according to the array elements, automatically identify the array length, automatically declare, and open up space on the heap to store object elements.

His {1, 2, 3, 4, 5} belongs to the object

Note: when allocating space to an array, you must specify the number of elements that the array can store to determine the size of the array. You cannot modify the size of an array after it is created. You can use length   Property gets the size of the array.

1.2 dynamic initialization

int[] nums=new int[3];
nums[0]=0;
nums[1]=1;
nums[2]=2;
nums.length;//The length here belongs to his own method.

num.length is the method that the object calls itself. Specify the length and initialize dynamically.

System.out.println(array+"Is the array in JVM On the stack, with the variable name array The address of the stored array, after hashing, is not the real address");
//The output result is: hashed address:
//[I@123772c4
		
//		//JAVA is very secure,
//		You can't get the address on the JAVA stack
//
//		Second: can you get it
//		The address on the heap,
//		But the address on the heap you get is hashed

2. Traversal of array

foreach is one of the four loops commonly used to traverse arrays

	for(int i:array0)//To the left of the colon is the type of element in the array~
	{
		System.out.println(i);
	} //foreach traverses the array, where int is the data type describing each element in the array, and i is not the subscript of the array element, but each element in the array

3. Common exceptions in arrays:

3.1,   NullPointerException   Null pointer exception

reason:   The reference type variable does not point to any object, but accesses the object's properties or calls the object's methods.

int[] a2=new int[0];
int j=a2.length;
System.out.println(j);
		 

The output here is 0

		int[] a3= null;
		System.out.println(a3[0]);

java.lang.NullPointerException

//The length of the empty array is 0, but if the reference is empty, and if the specified array reference is empty, the methods in this class will throw a NullPointerException, unless otherwise specified.
 

3.2, ArrayIndexOutOfBoundsException   Index value is out of bounds.

int[] a=new int[]{0,1,2,3,4};		
System.out.println(a[6]);

Reason: a nonexistent index value was accessed

 java.lang.ArrayIndexOutOfBoundsException: Index 6 out of bounds for length 5

4. Arrays: tool class for manipulating arrays.

4.1 java.util.arrays: many practical methods

4.1.1Arrays.toString();

Output the array in the array as a string.

        {
            int[] a0=new int[]{0,1,2,3,4,5,6};
            for (int i:a0)
            {
            	System.out.print(i+"  ");
            }
            System.out.println();
        	System.out.println(Arrays.toString(a0));
        }

4.1.2 the difference between the output of foreach traversal and the return value of tostring.

5. There are four shallow copy methods in the array:

 

5,1

    public static void main(String[] args)
    {

        {//1 direct transmission
            int[] a=new int[]{0,1,2,3,4,5,6};
            int[] a0=new int[a.length];
            a0=a;
            System.out.println();
        	System.out.println(Arrays.toString(a0));
        }

        {//2
            int[] a1={0,1,2,3,4,5,6};
            int[] ret=Arrays.copyOf(a1, a1.length);
            System.out.println(Arrays.toString(ret));
        }

        {//3
            int[] a2=new int[7];
            for (int i=0;i<7;i++)
            {
                a2[i]=i;
            }
            int[] rest1=new int[a2.length];
            System.arraycopy(a2, 0, rest1, 0,a2.length );
            System.out.println(Arrays.toString(rest1));
        }
        
        {//4
        	int[] a3=new int[] {0,1,2,3,4,5,6};
        	int[] ret1=a3.clone();
        	System.out.println(Arrays.toString(ret1));
        }
    }

The results are as follows:

 

  There are four ways to copy arrays

1. for circular copy

2,Arrays.copyOf

3,  System.arraycopy

This method belongs to the native method. The bottom layer is written in C language and C + + code. The copy speed should be the fastest of the above four!

4. Clone (): a copy of the Object is generated. This method is the clone method of the Object

In essence, they are shallow copies

There are four shallow copies, and there are corresponding deep copies. Subsequent blog posts will deeply analyze the four shallow copies and their corresponding deep copies

 

Keywords: Java C array

Added by malcome_thompson on Mon, 25 Oct 2021 13:00:23 +0300