Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Display ArrayList elements as columns in 2d ArrayList

Tags:

java

arraylist

I have an ArrayList like this:

ArrayList<ArrayList<Double>> someArray = new ArrayList<ArrayList<Double>>();

It has values like this:

[1,3,2,1,3,4,5,5],[7,2,1,3,4,5,4,2,4,3],....

I want the output to look like this:

1 7 . . .
3 2 
2 1
1 3
.
.
.

i.e each list in the ArrayList should be displayed as one column. I tried various ways but code displays them in rows..

for(int i=0;i<someArray.size();i++){
    System.out.println(someArray.get(i));
}

Looks simple but unable to figure it out :-!

like image 945
SuperStar Avatar asked Mar 23 '26 12:03

SuperStar


2 Answers

You need two loops - one to go through the inner ArrayList and one for the outer. You also don't know how long the largest list is (unless you're assuming they're all the same size).

You have to print line by line, so you need to iterate over all the lists at a given index and print them out on the same line.

int maxSize = 0;
for (ArrayList<Double> innerList : someArray) {
    if (maxSize < innerList.size()) {
        maxSize = innerList.size();
    }
}

for (int i = 0; i < maxSize; i++) {
    for (ArrayList<Double> innerList : someArray) {
        //You don't need this if all lists are the same length.
        if (i > innerList.size() - 1) { 
            System.out.print("x ");
        } else {
            System.out.print(innerList.get(i) + " ");
        }
    }
    System.out.println(); //new line for the next row
}
like image 120
ggmathur Avatar answered Mar 26 '26 02:03

ggmathur


This worked for my test. It printed:

5.5 2.5 2.5 
6.5 3.5 x 
x 4.5 x 
x x x 

And now the algorithm:

package com.sandbox;


import java.util.ArrayList;

public class Sandbox {

    public static void main(String[] args) {
        ArrayList<ArrayList<Double>> someArray = new ArrayList<ArrayList<Double>>();
        someArray.add(new ArrayList<Double>());
        someArray.get(0).add(5.5);
        someArray.get(0).add(6.5);
        someArray.add(new ArrayList<Double>());
        someArray.get(1).add(2.5);
        someArray.get(1).add(3.5);
        someArray.get(1).add(4.5);
        someArray.add(new ArrayList<Double>());
        someArray.get(2).add(2.5);

        boolean elementsLeft = true;
        int column = 0;
        while (elementsLeft) {
            for (ArrayList<Double> subList : someArray) {
                if (subList.size() > column) {
                    System.out.print(subList.get(column) + " ");
                }else {
                    System.out.print("x ");
                }
            }
            System.out.println();

            elementsLeft = isElementsLeft(someArray, column);
            column++;
        }
    }

    private static boolean isElementsLeft(ArrayList<ArrayList<Double>> someArray, int column) {
        for (ArrayList<Double> subList : someArray) {
            if (subList.size() > column) {
                return true;
            }
        }
        return false;
    }
}
like image 43
Daniel Kaplan Avatar answered Mar 26 '26 02:03

Daniel Kaplan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!