Adding Multiple Values in ArrayList at a single index

前端 未结 7 504
伪装坚强ぢ
伪装坚强ぢ 2021-01-12 01:35

I have a double ArrayList in java like this.

List values = new ArrayList(2);

Now what I want t

7条回答
  •  感情败类
    2021-01-12 01:44

    You can pass an object which is refering to all the values at a particular index.

    import java.util.ArrayList;
    
    public class HelloWorld{
    
     public static void main(String []args){
    
    
    ArrayList a=new ArrayList();
    
    a.add(new connect(100,100,100,100,100));
    
    System.out.println(a.get(0).p1);
    
    System.out.println(a.get(0).p2);
    
    System.out.println(a.get(0).p3);
    }
    
    }
    
    class connect
    {
    int p1,p2,p3,p4,p5;
    
    connect(int a,int b,int c,int d,int e)
    {
    
    this.p1=a;
    
    this.p2=b;
    
    this.p3=c;
    
    this.p4=d;
    
    this.p5=e;
    }
    
    }
    

    Later to get a particular value at a specific index, you can do this:

    a.get(0).p1;
    
    a.get(0).p2;
    
    a.get(0).p3;.............
    

    and so on.

提交回复
热议问题