Everyone will experience large and small interviews. Preparation for Java interviews will definitely involve several relatively large module test points. It can be said that array is an independent module in Java, and the knowledge points in it have formed a complete knowledge structure. In the actual inspection, it will involve discussion of variables, actual application of algorithms, etc. Below we will discuss common Java array interview questions. Bring sharing.
1. Basic knowledge
1. Do Java variables have to be initialized?
uncertain. A Java array variable is a reference data type variable. It is not the array object itself. As long as the array variable points to a valid array object, the array variable can be used. Initializing an array does not initialize the array variable, but initializes the array object - that is, allocating a continuous memory space for the array object, and this continuous memory space is the length of the array.
2. Are all basic type variables placed in stack memory?
wrong. It should be said like this: All local variables are stored in stack memory, whether they are basic type variables or reference type variables, they are stored in their respective method stack areas; but the objects referenced by reference type variables (including Arrays, ordinary Java objects) are always stored in heap memory.
3. When is a reference variable just the variable itself in the stack memory, and when does it become a Java object that references an instance?
A reference variable is essentially just a pointer. As long as the program accesses a property through a reference variable, or calls a method through a reference variable, the reference variable will be replaced by the object it refers to.
2. Example code
Rotate array:
For example: the element in the array is 123456, and after flipping it is 561234
Given an array of length n, it is required to move the last k elements to the front and the first nk elements to the back.
//First improve the method in interview question 1, change the flip from beginning to end to flip from i to j public int[] changeArray2(int[] array, int i, int j) { int temp = 0; while (i < j) { temp = array[i]; array[i] = array[j]; array[j] = temp; i++; j--; } //Rotating the array can be achieved in the following ways //1. Flip the first half 2. Flip the second half 3. Reverse the entire array public int[] swap(int[] array, int k) { int n = array.length - 1;// n=5 changeArray2(array, 0, n - k); changeArray2(array, n - k + 1, n); changeArray2(array, 0, n); return array; } }
The above is a display of interview questions in Java arrays. When reviewing arrays, you must have a clear understanding of the most basic knowledge structure, and at the same time, you must take into account some easy test points when reviewing.