2

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 :-!

6
  • Are there ever only 2 elements in the List? Commented Sep 17, 2013 at 20:54
  • Nope! Its variable..it can contain upto 50 elements(ArrayLists) in that ArrayList.. Commented Sep 17, 2013 at 20:57
  • println is a print \n (print + newline). If you want to print without going to the next line, use System.out.print instead. Commented Sep 17, 2013 at 20:58
  • Will the size of the nested lists always be the same? Commented Sep 17, 2013 at 21:01
  • 1
    I think the requirements of this question could be better spelled out to be more convenient to the answer-ers. Commented Sep 17, 2013 at 21:21

3 Answers 3

8

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

This gave me wrong output! The elements of each AraayList are not printed as column.. Thanks for help though!
The System.out.println was in the wrong place. I have updated it now and it works correctly.
3

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;
    }
}

2 Comments

+1 But you need spacer if nothing to print i.e 4.5 should be in 2nd column
O(n^2 m) instead of O(nm), submethods on a simple loop problem, trailing invisible whitespace at the end of every line, using while loops with awkward loop conditions instead of a for loop.... Sorry, little things that don't matter about code bother me.
1

You want to loop the nested list, not the outer list.

//Make sure at least 1 list to avoid a null reference on the next line
if(someArray.size() > 0) {
    //Assuming that all the nested lists are the same length
    for(int i = 0; i < someArray.get(0).size(); i++) {
        for(ArrayList<Double> nestedList : someArray) {
            if(list != someArray.get(0))
                System.out.print(" ");
            System.out.print(nestedList.get(i));
        }
        System.out.print("\n");
    }
}

Edit:

I assumed the question contained a typo that comes with someone hurriedly coming up with an example. If they are variable in length, please see ggmathur's answer.


Edit on an edit:

Okay I'm putting my own implementation for completeness.

String spacer = "X";
int maxSize = 0;
for(ArrayList<Double> nestedList : someArray)
    maxSize = (maxSize > nestedList.size()) ? maxSize : nestedList.size();

if(maxSize > 0) {
    for(int i = 0; i < maxSize; i++) {
        if(list != someArray.get(0))
            System.out.print(" ");
        System.out.print(nestedList.size() >= i ? nestedList.get(i) : spacer);
    }
    System.out.print("\n");
}

9 Comments

You can't assume that the nested lists are the same size.
Says his example values in the question
+1 that's a simple null validity check (you don't have to feed the OP with a spoon) - he's got the answer alright
@alfasin how do you decide when to stop looking in the next sublist? If the first sublist is shorter than the second, won't this stop short of printing everything?
@tieTYT if I understand correctly (from his example and one comment) then he's saying that the number of sub-lists (inner lists) may vary, not that the lists will have different number of elements.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.