Adding multiple values ​​to an ArrayList at a single index

I have a double ArrayList in java like this.

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

Now what I want to do is add 5 values ​​in the zero index of the list and 5 values ​​in the index one per cycle.

A null index would have the values {100,100,100,100,100} Index 1 would have the values {50,35,25,45,65}

and all these values ​​are stored in a double array in the following order

 double[] values = {100,50,100,35,100,25,100,45,100,65} 

How can i do this?

+6
source share
7 answers

@Ahamed makes sense, but if you insist on using lists, you have three of these types:

 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 these values ​​are listed 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 arrays are the same size, otherwise you need to use two for the operator)

 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]); } 
+7
source
 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 arrObjList . Hope this helps you.

+3
source

What about

  • First add the desired result as arraylist and
  • and convert to double array as you want.

Try it 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]); } } } 
+3
source

create an easy way to do this for you:

 public void addMulti(String[] strings,List list){ for (int i = 0; i < strings.length; i++) { list.add(strings[i]); } } 

Then you can create

 String[] wrong ={"1","2","3","4","5","6"}; 

and add it using this method to your list.

+3
source

Use a two-dimensional array instead. For example, int values[][] = new int[2][5]; Arrays are faster when you manipulate a little.

+1
source
 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
source

You can pass an object that refers to all values ​​in a specific 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 specific value at a specific index, you can do this:

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

etc.

0
source

Source: https://habr.com/ru/post/925891/


All Articles