Basics of arrays -1.5Java

1 Introduction

Each programming language has its own array concepts, similar, is to store a bunch of data, the Java array is used to store the same type of data, such as a statement arr [10] array can be used instead declare 10 variables.

 

2 statement and create an array

Before using an array, you must declare and create an array, the following look at how to declare and create an array.

Declare an array

dataType [ ] Array ; // preferred method

or

dataType Array [ ] ; // same effect, but not the preferred method, in order to let the C or C ++ programmers to more quickly understand Java

Example:

int[] array;
或
int array[];

 

Creating an array

method one:

array = new dataType[arraySize];

Creating type dataType, the number of array arraySize, and assigned to the array variable.

Example: array = new int [10];

 

Method Two:

array = {value0, value1, value2 ... valuek};

Directly create arrays and to assign each element in the array value0, value1, value2 ... valuek.

Example: array = {10, 20, 30, 40, 50};

Description:

array[0] -> 10

array[1] -> 20

array[2] -> 30

array[3] -> 40

array[4] -> 50

An array is accessible through the array index, from 0 to arraySize-1, such as Array [0] to array [arraySize-1].

 

We can direct the following manner, declare and create an array:

int [] Array = new new  int [10 ]; 

// course be the following three methods 
int Array [] = new new  int [10 ];
 int Array [] = {10, 20 is, 30 };
 int [] Array = {10, 20, 30};

 

Use Array

Since the array element type and size of the array is determined, it can be used directly for loop using an array or enhanced for loop.

public  class of MyList { 

    public  static  void main (String [] args) {
         int [] = {mylist 2,. 3,. 4,. 5,. 6,. 7,. 8 }; 

        // Common for loop 
        for ( int I = 0; I <mylist.length; I ++ ) { 
            System.out.println (mylist [I]); 
        } 

        System.out.println ( "---------------------- - Golden line -------------------------- " ); 
        
        // enhanced for loop 
        for ( int Item: mylist) { 
            the System. Out.println (Item); 
        } 
    } 

}

 

4 multidimensional array

Multidimensional arrays can be seen as an array of arrays, for example, it is a special two-dimensional array of one-dimensional array, a two-dimensional array each element is a one-dimensional array.

E.g.  int [] [] = {{List 2, 3,. 4}, {12 is, 13 is, 14}};  is a  new new int [2] [3]  two-dimensional array, creating a two-dimensional length 3 array.

Let's look instance:

public  class MyFirst { 
    
    public  static  void main (String [] args) {
         int an array of lSize = 2 ;
         int jsize =. 5 ;
         // declare a two-dimensional array, and creates a two-dimensional array and assignment 
        int [] [] = arrList new new  int [ 2] [5 ]; 
        
        // assignment and prints a two-dimensional array in each of the values 
        for ( int I = 0; I <an array of lSize; I ++ ) {
             for ( int J = 0; J <jsize; J ++ ) {
                 // by creating a random decimal Math.random (), and converted to integer multiplied by 10, it indicates the acquired random integer from 0 to 9 
                arrList [I] [J] = new newDouble (Math.random () * 10 ) .intValue (); 
                System.out.println ( "array elements arrList [" + i + "] [" + j + "] is:" + arrList [I] [J ]); 
            } 
        } 
        
        // sum of all the values of the calculated two-dimensional array 
        int sUM = 0 ;
         for ( int I = 0; I <an array of lSize; I ++ ) {
             for ( int J = 0; J <jsize; J ++ ) { 
                sUM = + arrList [I] [J]; 
            } 
        } 
        System.out.println ( "SUM:" + SUM); 
        
        // comparing the maximum value of a two-dimensional array 
        int max = arrList [0] [0];
        for(int i=0; i<isize; i++) {
            for(int j=0; j<jsize; j++) {
                if (max < arrList[i][j]) {
                    max = arrList[i][j];
                }
            }
        }
        System.out.println("max:" + max);
        
    }

}

 

5 Practice

By the multiplication table for loop to print out and save to a two-dimensional array multiplication table each result obtained.

In this exercise, we need to learn how to analyze problems, how to put a big problem into a number of small problems, and then one by one break. This is the most basic of our future projects way of thinking. Here, we may need another way of thinking, results-oriented way of thinking.

The results show:

 

Ideas:

1. First we have to imagine the appearance of the final print out the multiplication table, or a structure that is results-oriented way of thinking.

2. The need to be a two-dimensional array to store, then the size of a two-dimensional array certainly have identified a  new new int [9] [9]  .

3. Here a total of nine nine rows of data, so we can wrap a cycle is represented by a length of 9 rows 9, in a cycle represented in each column and then a small loop row.

4. Through observation, we can find the number of columns in each row are different, so we need for each cycle after the completion of one line, so that the number of columns plus one.

 

 code show as below:

public  class of MyList { 

    public  static  void main (String [] args) {
         int minVal =. 1 ;
         int maxVal =. 9 ;
         int size =. 9 ;
         int [] [] = List new new  int [size] [size]; 
        
        // for each row the large circle 
        for ( int I = minVal; I <= maxVal; I ++ ) {
             // row in each column of small cycles 
            for ( int J =. 1; J <= I; J ++ ) { 
                List [J -1] [I -1] = J * I; 
                of System.out.print (J+ "*" + i + "=" + list[j-1][i-1] + "    ");
            }
            System.out.println();
        }
        
    }

}

 

Guess you like

Origin www.cnblogs.com/sky-yemeng/p/11291240.html