Five, Java ----- array

Array definition

 

An array (Array) is one of the simplest types of complex data, which is an ordered set of data, each element of the array have the same data type can be used and a uniform array of different names to determine the array index unique Elements. According to the dimensions of the array can be divided into one-dimensional arrays, two-dimensional arrays and multidimensional arrays, etc.

Before defining the data, when most of them are used to store variable data
how large amounts of data, if appear in our program do?
Enter several numbers, enter several coordinate points, in general creates more variables to store these data, it is too much trouble.
These variables are basically common type, then we can use a container to manage all the numbers. Similar to the string, the string is actually a container of several characters in it, "abc" can be obtained through the index which one character / subscript. [1,2,3,4,5] similar string may be able to get a number by which certain index / subscript it? So we call this container array.

Note: Once the array is created, its size is fixed. Using a variable array reference to the array elements accessed by the subscript 

The nature of the array

Array is a series of space equal and continuous piece of memory address space 

Why space is equal to it? Is to facilitate the reunification maintain our data, have to ensure that the type of data between the same. (The same type of a plurality of variable space together form a structure called array)

Why address space is continuous variables it? It is to facilitate a unified operating our data. (See map memory array) 

Array format:

Data Type [] = new Array name Data type [length];

Example:

int[] arr = new int{a,b,c,d};

int[] arr = {a,b,c,d};

 The default value of the array:

  • The default integer type 0
  • Floating-point types default 0.0
  • Boolean Default false
  • Reference data types (objects) default null
  • char type defaults to '\ u0000'

 

Creating an array:

    Data Type [] = new Array name Data type [length]; (only creates an array of the specified length, but do not specify the content)
    data type [] = new Array Name Data Type [] {1,2,3,4,5}; ( create an array of the specified content (specified length))
    data type [] array name = {1,2,3,4,5}; (specified content creates an array (specified length))
    [] represents a one-dimensional array [] [] represents Two-dimensional array

An array of common errors

ArrayIndexOutOfBoundsException bounds array subscript (subscript exceeds a defined length)

Code Example:


public class Array error1 {

    public static void main(String[] args) {
 
       int[] a=new int[5];     //定义长度为5

       int[] a={1,2,3,4,5};

       System.out.println(a[6]);     //输出a[6]将报错 

    } 

}

 NullPointerException null pointer exception

Code Example:


public class Array error2 {

    public static void main(String[] args) { 

       int[] a=new int[5];      

       int[] a=null;     

       System.out.println(a[0]);     //数组对象没有任何变量引用

    }
}

The basic operation of the array

Traversal of the input and output array traversal

Assignment of the array assignment

Maximum / minimum value calculation array maximum, minimum

Traversal 

import java.util.Scanner;
class test1{
    public static void bianli(String[] args){
        int[] arr={1,2,3,4,5,6,7,8,9};
        System.out.println(arr.length);
        for(int i=0;i<arr.length;i++){
            System.out.println(arr[i]);
        }
    }
}

Note: the length of the array is only a single attribute array length

Assignment


public static void fuzhi(){
        Scanner scanner=new Scanner(System.in);
         //System.out.print("请输入10个数字:");
        int[] arr2=new int[10];
        for(int i=0;i<arr2.length;i++){
            System.out.print("请输入1个数字:");
            arr2[i]=scanner.nextInt();
        }
        for(int i=0;i<arr2.length;i++){
            System.out.print(arr2[i]+" ");
        }
    }

Selecting the maximum value. Minimum


public static void maxormin(){
        //计算最大值或最小值的 值
        //计算最大值或最小值的 角标
        //需求 获取最大的值10 获取最小值的角标4
        int[] arr={10,2,8,3,1,6,4,7,9,5};
        int max=arr[0];
        int min_index=0;
        for(int i=0;i<arr.length;i++){
            if(arr[i]>max){
                max=arr[i];
            }
            if(arr[i]<arr[min_index]){
                min_index=i;
            }
        }
        System.out.println("最大值"+max);
        System.out.println("最小值角标"+min_index);

    }

Array of search methods

Linear search : Find sequentially contrast, the best case to check out once; worst case check out until they have finished, it is also possible Mozhe number; when the length of the array, then the greater the worst case worse, the longer time required

Binary Search : The number of intermediate numbers go first to compare the array, will need to find, than the middle of the left will not find big and small will be intermediate values than the number on the right does not look. Until you find the number, or the entire array is looking over end

(Fibonacci Find)

Linear search Example:


 

class Text01{
    //线性查找
    public static void main(String[] args){
        int[] arr={10,2,8,3,1,6,4,7,9,5};  //初始化数组arr[]
        int key=2;     //key表示所查找的值
        int index=-1;   //当index=-1时,表示key元素不存在
        for(int i=0;i<arr.length;i++){
            if(arr[i]==key){
                index=i;
                break;
            }
        }
        System.out.println(index);  //输出所查找的值在数组中的角标
    }

 

 Binary search example:


 

public static void binarySearch(){
        //二分查找有个前提 数组必须有序
        /* 
        最好情况 查46 1次就出来了
        最坏情况 查12或者60 
        */
        int[] arr={12,17,21,32,38,41,46,49,50,50,51,59,60}; //初始化数组
        int key=46;                        //所查的值
        int index=-1;                      //输出的结果,若为-1表示没有找到key值
        int min_index=0;                   //左边指向的数字的角标
        int max_index=arr.length-1;        //右边指向的数字的角标
        int mid_index=(min_index+max_index)/2;   //中间所指向数字的角标
        while(arr[mid_index]!=key){            //利用while循环判断arr[mid_index]是否是key值
            if(key<arr[mid_index]){
                max_index=mid_index-1;
            }
            if(arr[mid_index]<key){
                min_index=mid_index+1;
            }
            if(min_index>max_index){
                break;
            }
            mid_index=(min_index+max_index)/2;
        }
        System.out.println(mid_index);   //输出角标 
  }

 

Released eight original articles · won praise 0 · Views 180

Guess you like

Origin blog.csdn.net/q1220668269/article/details/104376868