Convert Arraylist into 2D Array Containing Varying Lengths of Arrays

Convert ArrayList into 2D array containing varying lengths of arrays

String[][] array = new String[arrayList.size()][];
for (int i = 0; i < arrayList.size(); i++) {
ArrayList<String> row = arrayList.get(i);
array[i] = row.toArray(new String[row.size()]);
}

where arrayList is your ArrayList<ArrayList<String>> (or any List<List<String>>, change the first line inside the for loop accordingly)

How to convert a ArrayList into 2D array in Java 7

You can use .charAt() to get the character of the position in every string.

List<String> list = Arrays.asList("1110", "1010", "1011", "1110");
int[][] array = new int[list.size()][list.get(0).length()];
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < list.get(i).length(); j++) {
array[i][j] = list.get(i).charAt(j)-'0';
}
}

Convert two-dimensional ArrayList to two-dimensional array

You can do it as follows:

int[][] arr = list.stream()
.map(l -> l.stream()
.mapToInt(Integer::intValue)
.toArray())
.toArray(int[][]::new);

Each inner List is mapped to an int[] (by first converting it to an IntStream), and then you convert your Stream<int[]> to an int[][].

Converting 2D ArrayList to 2D Array

Problems

  1. First of all, ec here is of type ArrayList<Double[]>, which means ec.get(i) should return Double[] and not ArrayList<Double>.
  2. Second, double and Double are completely different types. You can't simply use row.toArray(new double[row.size()]) on your code.

Solutions

1.

If you want a true 2D ArrayList of Doubles then the type of ec should be ArrayList<ArrayList<Double>>. But because we can't use toArray(), we manually loop instead.

public ArrayList<ArrayList<Double>> ec;  // line changed here
public double[][] ei;
...
encogCorpus = new ArrayList<ArrayList<Double>>(); // and here also
...
ec.add(inputs); // `inputs` here should be of type `ArrayList<Double>`
...
ei = new double[ec.size()][];

for (int i = 0; i < ec.size(); i++) {
ArrayList<Double> row = ec.get(i);

// Perform equivalent `toArray` operation
double[] copy = new double[row.size()];
for (int j = 0; j < row.size(); j++) {
// Manually loop and set individually
copy[j] = row.get(j);
}

ei[i] = copy;
}

2.

But if you insist of using ArrayList<Double[]>, we only need to change the main part:

public ArrayList<Double[]>  ec;
public double[][] ei;
...
encogCorpus = new ArrayList<Double[]>();
...
ec.add(inputs);
...
ei = new double[ec.size()][];

for (int i = 0; i < ec.size(); i++) {
// Changes are only below here

Double[] row = ec.get(i);
double[] copy = new double[row.length];

// Still, manually loop...
for (int j = 0; j < row.length; j++) {
copy[j] = row[j];
}

ei[i] = copy;
}

3.

Finally, if you could change Double[] to double[], solution 2 would become,

public ArrayList<double[]>  ec; // Changed type
public double[][] ei;
...
...
for (int i = 0; i < ec.size(); i++) {
// Simpler changes here
ei[i] = ec.get(i).clone();
}
...

How to split an arraylist into two dimensional arrays?

You must split each element of the List separately, since split operates on a String and returns a 1-dimensional String array :

File file=new File("text/file1.txt");
ArrayList<String> lines= (ArrayList<String>) FileUtils.readLines(file);
String [][] array=new String[lines.size()][];
for (int i=0;i<lines.size();i++)
array[i]=lines.get(i).split(",");

Converting an ArrayList into a 2D Array

I managed to find "a way" to do so, knowing the number of attributes each contacts has (6). So considering an ArrayList listofContacts

    int numberOfContacts = listofContacts.size()/6;
Object[][] newArrayContent = new Object[numberOfContacts][6];

for(int x = 0; x<numberOfContacts; x++){
for(int z = 0; z < 6; z++){
int y = 6 * x;
newArrayContent [x][z] = list.get(y+z);
System.out.println(newArrayContent [x][z].toString());
}
}

Convert Nested List of Integers to a two dimensional array using Streams ( ListListInteger - int[][] )

You only need to make a few modifications to the String[][] solution:

List<List<Integer>> lists = ...;
int[][] arrays = lists.stream() // Stream<List<Integer>>
.map(list -> list.stream().mapToInt(i -> i).toArray()) // Stream<int[]>
.toArray(int[][]::new);

The mapToInt(i -> i) is unboxing each Integer (i.e. Integerint).

See:

  • Stream#mapToInt(ToIntFunction)
  • IntStream#toArray()


Related Topics



Leave a reply



Submit