Java arrays, traversing arrays

Source: Internet
Author: User

Arrays: A set of identical data types (containers)

1. Array declaration format: data type [] Array name = new data type [length];

Once the array length is determined, it cannot be changed.

The data in the array must be of the same type or auto-upward-transition-compatible type

2. Array initialization

1) Static initialization

Example: int [] i= {1,2,3,4, ' a '};

Float [] f = {1,2,3,4.0f};

2) non-static initialization

Example: String [] str = new STRING[3];

Str[0]= "Zhang";

Str[1]= "Zhan";

Str[2]= "Zha";

Two-dimensional arrays

Format: data type [] array name =new data type [row] [column];

int [] [] I =new int[one-dimensional array length [two-dimensional array length];

Array initialization

1) Static initialization

Int[][] I = {{1,2,3},{4,5,6}};

2) non-static initialization

int[][] i = new int[2][3];

I[0][0]=1;

i[0][1]=2;

i[0][2]=3;

i[1][0]=4;

i[1][1]=5;

i[1][2]=6;

Three-dimensional array: Int[][][]arr = {{1,2},{3,4,5},{6}},{{7,8},{9}},{{10,11},{12,13}}}

Arr.length

Arr[i].length

Arr[i][j].length

Traversing arrays: 1.for, 2.while, 3.do...while, 4. Enhanced for Loop (foreach)

1. Enhanced for Loop: for (data type variable: Array (collection)) {

Output (variable);

}

 Public classdemo13{ Public Static voidMain (String []args) {//one-dimensional arraysString [] str =NewString[3]; str[0]= "Zhang San"; str[1]= "John Doe"; str[2]= "Harry"; //For- form traversal array               for(inti=0;i<str.length;i++) {System.out.println ("One-dimensional array: for:" +Str[i]); }            //enhanced for Form s traversal of all arrays              for(String s:str) {System.out.println ("One-dimensional array enhancement: for:" +s); }            //do...while Form              intIi=0;  Do{System.out.println ("One-dimensional array: do...while:" +Str[ii]); II++; } while(ii<str.length); //While form             intIii=0;  while(iii<str.length) {System.out.println ("One-dimensional array: while:" +STR[III]); III++; }        //two-dimensional arrays        int[] in =New int[2] [3]; in[0][0]=1; in[0][1]=2; in[0][2]=3; in[1][0]=4; in[1][1]=5; in[1][2]=6; //For form         for(inti1=0;i1<in.length;i1++){             for(intj=0;j<in[i1].length;j++) {System.out.println ("Two-dimensional array: for:" +In[i1][j]); }          }        //Enhanced for form         for(int[] i2:in) {             for(intk:i2) {System.out.println ("Two-dimensional array: Enhanced for:" +k); }        }        //While form        intI3=0;  while(i3<in.length) {             for(intj1=0;j1<in[i3].length;j1++) {System.out.println ("Two-dimensional array: while:" +in[i3][j1]); } i3++; }        //do...while Form        intI4=0;  Do{             for(intj2=0;j2<in[i4].length;j2++) {System.out.println ("Two-dimensional array: do...while:" +In[i4][j2]); } I4++; } while(i4<in.length);/*        int[][]in1={{1,2,3},{4},{5,6},{7,8,9,10}};                for (int [] k:in1) {//Enhanced for for (int l:k) {System.out.println (L); }} for (int i=0;i<in1.length;i++) {//For form for (int j=0;j<in1                [i].length;j++) {System.out.println (in1[i][j]);                          }} int i=0;                    While form while (i<in1.length) {int j=0;                        while (j<in1[i].length) {System.out.println (in1[i][j]);                    j + +;                 } i++;                       } int i=0;                    Do...while form do{int j=0;                        do{System.out.println (In1[i][j]);                    j + +;               }while (j<in1[i].length);      i++; }while (i<in1.length);*/    }}

Java arrays, traversing arrays

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.