Older Dick Silk Self-study note--java 0 basics to rookie--013

Source: Internet
Author: User
Tags array definition

arrays, two-dimensional arrays, Yang Hui triangles

1. Arrays

An array is a collection of multiple elements that store the same data type.

Arrays can store either the base data type or the reference data type.

Array definition Format: data type [] array name//array must be initialized before you can use

Array dynamic initialization: data type [] Array name = new data type [array length];  such as: int[] a = new a[3]; Specify only the length of the array when initializing

Array static initialization 1: data type [] Array name = new data type []{element 1, ..., element n};  such as: int[] a = new a[]{1,2,3}; Specifies the initial value of each array element when it is initialized

Array static initialization 2: data type [] Array name = {element 1, ..., element n};  such as: int[] A = {£ º}; Specifies the initial value of each array element when it is initialized

The value of the array element: array name [index], such as: int[] A = {one-by-one};  int n = a[1]; n=2, the index of the array starts at 0, and the array is the reference type

1 classfin{2      Public Static voidMain (string[] args) {3         int[] A=int[3];4A[0]=1;5a[1]=2;6A[2]=3;7 8         int[] b=A;9B[1]=4;Ten  OneSystem.out.println (b)//the address of the output array B (same as the address of array a) ASystem.out.println (A[1])//a[1]=4 -     } -}

FAQ: Index out of bounds exception, null pointer exception.

Index out-of-bounds exception: The index of the array is greater than the number of elements in the array, accessing non-existent indexes, such as: int[] a = int[3];   int n = a[5]; Error

Null pointer exception: the array is no longer pointing to "heap" memory, still accessing the array, such as: int[] a = int[3];  n = null;  int n = a[0]; Error

Gets the length of the array: array name. length; Int

1 //reverse order of arrays2  Public static voidLiverpoolint[] arr) {3     intK=0;4      for(inti=0,j=arr.length-1;i<j;i++,j--){5k=Arr[i];6arr[i]=Arr[j];7arr[j]=K;8     }9}

2, two-dimensional array

A two-dimensional array is an array of elements that are one-dimensional arrays.

Definition of a two-dimensional array: data type [[]] array name

Dynamic initialization of two-dimensional arrays 1: data type [] Array name = new data type [m][n];  such as: int[][] a = new a[2][3]; M (2) a one-dimensional array of length n (3)

Dynamic initialization of two-dimensional arrays 2: Data type [] Array name = new data type [m][];  such as: int[][] a = new a[2][]; M (2) an uninitialized one-dimensional array

Static initialization of a two-dimensional array 1: data type [] Array name = new data type {{element ...}, ...}, {element ...}}; such as: int[][] a = new int[][]{{1,2,3}, {4,5}, {6}};

Static initialization of a two-dimensional array 2: data type [] Array name = {{element ...}, ...}, {element ...}}; such as: int[][] A = {{{)}, {4,5}, {6}};

Value of the two-dimensional array element: array name [index] [index], such as: int[][] A = {{4,5,6}};  int n = a[1][2]; N=6

3, Yang Hui triangle

The first column, the last column of each row, the value is 1, and the third row begins with the value of each column (except the last column) equal to the sum of the previous column and the previous row of the column, or A[m][n]=a[m-1][n-1]+a[m-1][n].

1 classtest{2      Public Static voidMain (string[] args) {3Yang (5);4     }5 6      Public Static voidYangintN) {    7         if(n>0){8             //two-dimensional arrays9             int[] y=New int[n][];Ten             if(n==1){ Oney[0]=New int[]{1}; A}Else if(n==2){ -y[0]=New int[]{1}; -y[1]=New int[]{1,1}; the}Else{ -y=New int[n][]; -y[0]=New int[]{1}; -y[1]=New int[]{1,1}; +                  for(inti=2;i<n;i++){ -y[i]=New int[I+1]; +                      for(intj=0;j<=i;j++){ A                         if(j==0| | j==i) { atY[i][j]=1; -}Else{ -Y[i][j]=y[i-1][j-1]+y[i-1][j]; -                         } -                     } -                 } in             } -             //Output to              for(inti=0;i<y.length;i++){ +                  for(intj=0;j<y[i].length;j++){ -System.out.print (y[i][j]+ ""); the                 } *System.out.println (""); $             }    Panax Notoginseng}Else{ -System.out.print ("bug");//N<1,bug the         } +     } A}

Older Dick Silk Self-study note--java 0 basics to rookie--013

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.