"Java from Beginner to Mastery" Chapter fifth study notes

Source: Internet
Author: User

5th Chapter Array

One or one-D arrays

1. Create

A one-dimensional array is essentially a set of data of the same type, and in Java an array is not a data type, but an object. Two ways to declare an array:

Int[] arr1;
int arr2[];

After the array declaration, you need to allocate the memory space before using it:

arr1=New int[3]; //array element number is 3, that is, the length is 3;arr2=new int[2];

The Declaration and allocation space can also be manipulated:

< Span class= "Hljs-number" >int arr1[]= new int[3];
int arr2[]=new int[2";

< Span class= "Hljs-number" >

arr1[]=New int[]{1,2,3};
arr2[]={4,5};

1 //find the smallest element in a one-dimensional array2  Public classArrayminnum {3    Public Static voidMain (string[] args) {4     intarr[]=New int[]{12,398,25,1024,414,1973,2,520,1013};5     intTEMPMIN=ARR[0];//the minimum value of the scratchpad, the initial value is the first element value6     intMarknumber=1;//Location Identifiers7      for(intj=1;j<arr.length;j++){8         if(tempmin>Arr[j]) {9tempmin=Arr[j];TenMarknumber=j+1; One         } A     } -System.out.println ("The minimum value of the array arr is" +tempmin+ "; is the" +marknumber+ "element in the array"); -   } the}

two or two-D arrays
Each element in a two-dimensional array is actually a one-dimensional array, creating, allocating space, and assigning values similar to:

int arr[][];                    // The declaration creates a arr=newint[2][3]               // allocates 2*3 element space arr={{1,2,3},{4,5,6}};              // Assign Value int arrx[][]=newint[][]{{1,2},{3,4},{5,6}};  // Create + assign + assignment
1 //transpose operations on a two-dimensional array2  Public classArrayswap {3      Public Static voidMain (string[] args) {4         intarr1[][]=New int[][]{{1,2,3},{4,5,6},{7,8,9}};5         intarr2[][]=New int[3] [3];6          for(inti=0;i<arr1.length;i++){7              for(intj=0;j<arr1[i].length;j++){8arr2[j][i]=Arr1[i][j];9             }Ten         } One          for(inti=0;i<arr1.length;i++){ A              for(intj=0;j<arr1[i].length;j++){ -System.out.print (arr1[i][j]+ ""); -             } theSystem.out.println (""); -         } -          for(inti=0;i<arr2.length;i++){ -              for(intj=0;j<arr2[i].length;j++){ +System.out.print (arr2[i][j]+ ""); -             } +System.out.println (""); A         } at     } -}

Iii. Basic operation of arrays
1. Traversing elements through the For method

//1, two-dimensional array of element traversal output Public classArraytrap { Public Static voidMain (string[] args) {intarr[][]=New int[][]{{1,3,5,7},{2,4,6},{0,10}}; System.out.println ("For loop general method:");  for(inti=0;i<arr.length;i++){             for(intj=0;j<arr[i].length;j++) {System.out.print (Arr[i][j]+"  "); } System.out.println (""); } System.out.println ("Foreach Method:");  for(intArrx[]:arr) {             for(intA:arrx) {System.out.print (a+"  "); } System.out.println (""); }    }}

2. Fill replacement array elements
(1) Fill (int[] a,int value) method: assigns value to each element in array a, and the return value of the method is the changed array a.
(2) Fill (int[] a,int fromindex,int toindex,int value): Assigns value to the element in array a, starting with an element indexed as FromIndex, to the end of an element indexed to Toindex excluding the element , the return value of the method is the changed array a.

1 //change the values of some elements in the array2 Importjava.util.Arrays;3  Public classArraydisplace {4      Public Static voidMain (string[] args) {5         intarr[]=New int[]{13,8,9,520,14,4,24,27};6SYSTEM.OUT.PRINTLN ("Array before padding is:");7          for(intX:arr) {8System.out.print (x+ "");9         }TenArrays.fill (arr,1,4,520); OneSystem.out.println ("The array after padding is:"); A          for(intX:arr) { -System.out.print (x+ ""); -         } the     } -}

3. Copying an array
(1) copyOf (Arr,int newlength): arr is the array to be copied. Newlength is used to specify the length of the new array after assignment, if the length is greater than the length of arr, the remainder is filled with 0 (integer array) or null (a char array). If the newlength is smaller than the arr length, it is intercepted by Newlength. The return value is a new array after the copy.
(2) Copyofrange (Arr,int fromindex,int toindex): The copy range starts with an element indexed as FromIndex, and the element is not included at the end of an index of Toindex, and the return value of the method is a new array after the copy.

Iv. examples

1 //sort array elements from small to large with bubbling method2  Public classBubblesortarray {3      Public Static voidMain (string[] args) {4         intourarray[]=New int[]{1973,10,1013,24,414,4,1980,520,13,19999};5         inttemp;6System.out.println ("Before sorting:");7          for(intX:ourarray) {8System.out.print (x+ "");9         }Ten          for(inti=1;i<ourarray.length;i++){ One              for(intj=0;j<ourarray.length-i;j++){ A                 if(ourarray[j]>ourarray[j+1]){ -Temp=ourarray[j+1]; -ourarray[j+1]=Ourarray[j]; theourarray[j]=temp; -                 } -             } -         } +System.out.println ("After sorting:"); -          for(intX:ourarray) { +System.out.print (x+ ""); A         } at     } -}
1 //Create an int type two-dimensional array, find the sum of the elements and print2  Public classArraysum {3      Public Static voidMain (string[] args) {4         intarr[][]=New int[][]{{13,414,73},{10,24,80},{2012,10,13},{520,13,14}};5         intSum=0;6          for(intA[]:arr) {7              for(intx:a) {8sum+=x;9             }Ten         } OneSystem.out.println ("Two-dimensional array arr each element sums to" +sum); A     } -}

"Java from Beginner to Mastery" Chapter fifth study notes

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.