The final keyword and an array of acquaintance

final key role:

 

Use an array

package shu;

public class Shuzu {
    public static void main(String args[]) {
        int[] a;
        int i;
        String b[]=new String[15];
        a=new int[10];
        for(i=0;i<a.length;i++) {
            a[i]=2*i;
            System.out.println(a[i]);
        }
        User arr1[]=new User[3];    //存放的是引用类型
        
//        The User new new = U the User ( "A", 1234568); 
        of arr1 [0] = new new the User ( "Sun", 123456);    // generates a target placed ,, of arr1 [] is the address, not the object 
        arr1 [1 ] = new new the User ( "AI", 455 ); 
        of arr1 [ 2] = new new the User ( "mentality", 4555 );
         for (I = 0; I <arr1.length; I ++ ) { 
            System.out.println (of arr1 [ I] .getName ()); 
        } 
        
    } 
} 
class the User {
     Private String name;
     Private  int pwd;
     public the User (String name, int pwd) {
         the this.name=name;
        this.pwd=pwd;
    }
    public void setName(String name) {
        this.name=name;
    }
    public String getName() {
        return this.name;
    }
    
}

Initialize an array There are three ways: static initialization, dynamic initialization, the default initialization

 

foreach loop for reading the value of the array element value of the element can not be modified.

package shu;

public class Shuzu {
    public static void main(String args[]) {
        int[] a;
        int i;
        String b[]=new String[15];
        a=new int[10];
        for(i=0;i<a.length;i++) {
            a[i]=2*i;
            System.out.println(a[i]);
        }
        User arr1[]=new User[3];    //存放的是引用类型
        
//        The User new new = U the User ( "A", 1234568); 
        of arr1 [0] = new new the User ( "Sun", 123456);    // generates a target placed ,, of arr1 [] is the address, not the object 
        arr1 [1 ] = new new the User ( "AI", 455 ); 
        of arr1 [ 2] = new new the User ( "mentality", 4555 );
         for (I = 0; I <arr1.length; I ++ ) { 
            System.out.println (of arr1 [ I] .getName ()); 
        } 
        System.out.println ( "************************************************************" ); 
        
        // the foreach circulation the read value of the array element value of the element can not be modified. 
        for (the User m: of arr1) { 
            System.out.println (m);   
        
class User {
    private String name;
    private int pwd;
    public User(String name,int pwd) {
        this.name=name;
        this.pwd=pwd;
    }
    public void setName(String name) {
        this.name=name;
    }
    public String getName() {
        return this.name;
    }
    
}

 

Guess you like

Origin www.cnblogs.com/ssxblog/p/11183807.html