文章90
标签1
分类38

JAVA-ArrayList

在不知道数组大小的情况的,可以使用ArrayList动态数组

    
    import java.util.ArrayList;
    //创建角色结构
    class Role{
        
        private String name;
        private int  type;


        public Role(int  type,String name){

this.type=type;
this.name=name;

        }

    public String getName(){
    
    
    return  name;
}
    



    public int getType(){
    
    
    return  type;
}
    
    }
    
    
    public class RoleMAIN {
        public static void main(String[] args) throws Exception {
//创建列表ArrayList<结构类> 名字;
    ArrayList<Role> arrrole;
    //初始化为空;
    arrrole=new ArrayList<Role>();

                int  type[]  =  {  9,8,7 };
                String  name[]  =  {  "a",  "b",  "c"  };

//增加方法 对象.add(new 结构类())
                for(int a=0;a<type.length;a++){

arrrole.add(new Role(type[a],name[a]));

                }


//获取的方法  对象.get(第几个).对应方法;

        for(int a=0;a<type.length;a++){

System.out.println(arrrole.get(a).getType()+" type");
System.out.println(arrrole.get(a).getName()+" name");


                }
/*
9 type
a name
8 type
b name
7 type
c name
*/



//设置方法    对象.set(第几个,结构类)
    
arrrole.set(0,new Role(888, "k"));

    for(int a=0;a<type.length;a++){

System.out.println(arrrole.get(a).getType()+" type");
System.out.println(arrrole.get(a).getName()+" name");

/*
888 type
k name
8 type
b name
7 type
c name

*/
                }

//






        }
    }

    0 评论

    ">