An array of basic knowledge of Java and related interview questions

An array definition:
The so-called arrays, in programming, in order to facilitate processing, the number of variables of the same type organized in an ordered form of a data format. These sets are arranged in a certain order of the same type of data is referred to an array. Each of the data array called array element, the array elements to store its index to indicate the location of the index starting from 0, the step is 1.
Embodiment 1: the array element type [] array name; int [ .] AGEs; it may be recommended int [] as a data type, int type array type.
embodiment 2: type array name [] array elements; int ages [];
two array initialization:
1. static initialization:
syntax:
array element type [] = new array array element type name [] {element 1, element 2, element 3, ...};
for example:
int [] the nums = new int [] {l, 3,5, 7,9};
simple wording, then must declare, once initialized, the first statement could not initialize:
int [] = {1,3,5,7,9} the nums;
2. dynamic initialization:
setting elements of the array by our the number (the array length), and the initial value of each array element are determined by the system.
syntax:
array element type [] = new array array element type name [length];
for example:
int [] = new int AGEs [100];
Note: static and dynamic initialization initialization can not be used simultaneously int [] nums = new int [5 ] {1,3,5,7,9}; error writing
three sides questions (written).
1. Find the maximum of an array of handwriting

//手写找出做大值
    static int getMax(int[] arr){
        if(arr!=null && arr.length>0){
            int max=arr[0];
            for (int i =0;i<arr.length;i++){
                if(max<arr[i]){
                    max=arr[i];
                }
            }
            return max;
        }
        return -1;
    }

2. bubble sort
This sorting method is the simplest, basic ideas:
for each element sequentially from start to finish unordered compare the magnitude relationship of two adjacent elements, then the switch position is greater than if, after a first comparison after sorting maximum can be drawn, and then use the same method to the remaining elements can be compared one by one.
If N elements can be seen, then conduct a total of N-1 comparator wheel, the first wheel to be M NM comparisons. (If the elements 6, to be 6-1 rounds comparison, comparison 6-1 times in the first round, the third round Comparative 6-3 times).

//冒泡排序法
    static  void  sort(int[] arr){
        int temp = 0;
        for (int i =0;i<arr.length-1;i++){
            for(int j = 0;j<arr.length-1-i;j++){
                if(arr[j]>arr[j+1]){
                    temp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=temp;
                }
            }
        }
    }

3. Select the sort
basic ideas: selecting an index position of the element, the rear element and then sequentially comparing, if more than the switching position, the first round of comparison after the minimum sort can be drawn, using the same method and then the remaining elements can compare apples to apples.
It can be seen selection sort, the minimum value will be selected in the first round, the second round will select a second small value, until the last.
The first round of [0] and the elements back from the ARR compared, from the second round arr [1] and the following elements compared, and so on. To make the number N of N-1 round. Select the sort only once per round exchange, with respect to the high efficiency of some bubble sort.

   //选择排序
    static void selectSort(int[] arr) {
        int temp = 0;
        if (arr != null && arr.length > 0) {
            for (int i = 0; i < arr.length - 1; i++) {
                for (int j = i + 1; j < arr.length; j++) {
                    if (arr[i] > arr[j]) {
                        temp = arr[j];
                        arr[j] = arr[i];
                        arr[i] = temp;
                    }
                }
            }
        }
    }

4. The binary search index element (array element must be sequential.)

//二分法查找
    static int binarySearch(int[] arr, int key) {
        int low = 0;
        int high = arr.length - 1;
        if (arr != null && arr.length > 0) {
            while (low <= high) {
                int mid = (low + high) >> 1;
                int midVal = arr[mid];
                if (midVal < key) {
                    low = mid + 1;
                } else if (midVal > key) {
                    high = mid - 1;
                } else {
                    return mid;
                }
            }
        }
        return -1;
    }

4. How to increase the length of the array:
A: Java in the array is a fixed-length, not dynamically increase the length. If you want to expand the array, only by redefining the array, copy the contents of the old array to the new array.

copyOf
public static int[] copyOf(int[] original,
                           int newLength)复制指定的数组,截取或用 0 填充(如有必要),以使副本具有指定的长度。对于在原数组和副本中都有效的所有索引,这两个数组将包含相同的值。对于在副本中有效而在原数组无效的所有索引,副本将包含 0。当且仅当指定长度大于原数组的长度时,这些索引存在。 
参数:
original - 要复制的数组
newLength - 要返回的副本的长度 
返回:
原数组的副本,截取或用 0 填充以获得指定的长度 
        int[] arr = {2, 1, 4, 3, 6, 5};
        int[] arr1= Arrays.copyOf(arr,10);
        System.out.println(Arrays.toString(arr1));

5.Arrays tools
common method

asList(T... a) 
          返回一个受指定数组支持的固定大小的列表。
binarySearch(int[] a, int key) 
          使用二分搜索法来搜索指定的 int 型数组,以获得指定的值 
copyOf(int[] original, int newLength) 
          复制指定的数组,截取或用 0 填充(如有必要),以使副本具有指定的长度。 
copyOfRange(int[] original, int from, int to) 
          将指定数组的指定范围复制到一个新数组。 
equals(int[] a, int[] a2) 
          如果两个指定的 int 型数组彼此相等,则返回 truefill(int[] a, int val) 
          将指定的 int 值分配给指定 int 型数组的每个元素。 
fill(int[] a, int fromIndex, int toIndex, int val) 
          将指定的 int 值分配给指定 int 型数组指定范围中的每个元素。 
hashCode(int[] a) 
          基于指定数组的内容返回哈希码 
sort(int[] a) 
          对指定的 int 型数组按数字升序进行排序。 
sort(int[] a, int fromIndex, int toIndex) 
          对指定 int 型数组的指定范围按数字升序进行排序。 
toString(int[] a) 
          返回指定数组内容的字符串表示形式。
                                            
Published 99 original articles · won praise 2 · Views 2630

Guess you like

Origin blog.csdn.net/weixin_41588751/article/details/105082310