0

I want to display the contents of an array of linked lists. Each linked list contains a String array. The content of the array of linked lists is displaying as [[[Ljava.lang.String;@15db9742], [[Ljava.lang.String;@6d06d69c], [[Ljava.lang.String;@7852e922]]. How do I solve this?

public class LinkedListOfStringArrays {
    public static void main(String[] args) {
        LinkedList<String[]> Values[] = new LinkedList[3];
        for (int i = 0; i < 3; i++){
            Values[i] = new LinkedList<String[]>();
        }
        String[] first = {"first element", "ABC"};
        String[] second = {"second element", "DEF"};
        String[] third = {"third element", "GHI"};
        Values[0].add(first);
        Values[1].add(second);
        Values[2].add(third);
        System.out.println(Arrays.toString(Values));
    }
}
2
  • iterate, then use Arrays.toString probably, another option (prefered) would be to use a custom class (with proper toString) for the pair of strings and not an array Commented Feb 13, 2016 at 19:33
  • LinkedList<String[]> Values[] ?? This creates an array of linked lists... Why do you want this? Commented Feb 13, 2016 at 19:36

2 Answers 2

2

You will have to loop through the lists and output it manually:

// Loop through the Array of LinkedLists
for(LinkedList<String[]> list : Values) {

    // Next loop through each of the lists
    for(String[] keyValuePair : list) {

        // Then output the values as you see fit
        System.out.println(keyValuePair[0] + " - " + keyValuePair[1]);
    }
}

This will give you the following output:

first element - ABC
second element - DEF
third element - GHI
Sign up to request clarification or add additional context in comments.

Comments

0

In Java 8 you can try as

Stream.of(Values).                                                     
flatMap(list -> list.stream()).                                
forEach(values -> System.out.println(String.join("-",values)));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.