Adding Multiple Values in ArrayList at a single index

前端 未结 7 448
伪装坚强ぢ
伪装坚强ぢ 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<connect> a=new ArrayList<connect>();
    
    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.

    0 讨论(0)
  • 2021-01-12 01:45

    Use two dimensional array instead. For instance, int values[][] = new int[2][5]; Arrays are faster, when you are not manipulating much.

    0 讨论(0)
  • 2021-01-12 01:52

    @Ahamed has a point, but if you're insisting on using lists so you can have three arraylist like this:

    ArrayList<Integer> first = new ArrayList<Integer>(Arrays.AsList(100,100,100,100,100));
    ArrayList<Integer> second = new ArrayList<Integer>(Arrays.AsList(50,35,25,45,65));
    ArrayList<Integer> third = new ArrayList<Integer>();
    
    for(int i = 0; i < first.size(); i++) {
          third.add(first.get(i));
          third.add(second.get(i));
    }
    

    Edit: If you have those values on your list that below:

    List<double[]> values = new ArrayList<double[]>(2);
    

    what you want to do is combine them, right? You can try something like this: (I assume that both array are same sized, otherwise you need to use two for statement)

    ArrayList<Double> yourArray = new ArrayList<Double>();
    for(int i = 0; i < values.get(0).length; i++) {
        yourArray.add(values.get(0)[i]);
        yourArray.add(values.get(1)[i]);
    }
    
    0 讨论(0)
  • 2021-01-12 01:53

    How about

    1. First adding your desired result as arraylist and
    2. and convert to double array as you want.

    Try like this..

    import java.util.ArrayList;
    import java.util.List;
    
        public class ArrayTest {
    
            /**
             * @param args
             */
            public static void main(String[] args) {
                // TODO Auto-generated method stub
    
                // Your Prepared data. 
                List<double[]> values = new ArrayList<double[]>(2);
    
                double[] element1 = new double[] { 100, 100, 100, 100, 100 };
                double[] element2 = new double[] { 50, 35, 25, 45, 65 };
    
                values.add(element1);
                values.add(element2);
    
                // Add the result to arraylist.
                List<Double> temp = new ArrayList<Double>();
                for(int j=0;j<values.size(); j++) {
                    for (int i = 0; i < values.get(0).length; i++) {
                        temp.add(values.get(0)[i]);
                        temp.add(values.get(1)[i]);
                    }
                }
    
                // Convert arraylist to int[].
                Double[] result = temp.toArray(new Double[temp.size()]);
                double[] finalResult = new double[result.length]; // This hold final result.
                for (int i = 0; i < result.length; i++) {
                    finalResult[i] = result[i].doubleValue();
                }
    
                for (int i = 0; i < finalResult.length; i++) {
                    System.out.println(finalResult[i]);
                }
            }
        }
    
    0 讨论(0)
  • 2021-01-12 01:53
    import java.util.*;
    public class HelloWorld{
    
     public static void main(String []args){
      List<String> threadSafeList = new ArrayList<String>();
        threadSafeList.add("A");
        threadSafeList.add("D");
        threadSafeList.add("F");
    
    Set<String> threadSafeList1 = new TreeSet<String>();
        threadSafeList1.add("B");
        threadSafeList1.add("C");
        threadSafeList1.add("E");
        threadSafeList1.addAll(threadSafeList);
    
    
     List mainList = new ArrayList();   
     mainList.addAll(Arrays.asList(threadSafeList1));
     Iterator<String> mainList1 = mainList.iterator();
     while(mainList1.hasNext()){
     System.out.printf("total : %s %n", mainList1.next());
     }
    }
    }
    
    0 讨论(0)
  • 2021-01-12 01:54
    ArrayList<ArrayList> arrObjList = new ArrayList<ArrayList>();
    ArrayList<Double> arrObjInner1= new ArrayList<Double>();
    arrObjInner1.add(100);
    arrObjInner1.add(100);
    arrObjInner1.add(100);
    arrObjInner1.add(100);
    
    arrObjList.add(arrObjInner1);
    

    You can have as many ArrayList inside the arrObjList. I hope this will help you.

    0 讨论(0)
提交回复
热议问题