0

there are many posts on how to display the elements of LinkedList<String>, but I can't find something that I understand for LinkedList<String[]>.

The following code shows the output: LinkedList:[[Ljava.lang.String;@5305068a, [Ljava.lang.String;@1f32e575

whereas I'm looking for the output: LinkedList:[[Audi SQ5,341], [LandRover Discovery,306]].

and I don't want to overwrite the toString().

Code:

LinkedList<String[]> cars2 = new LinkedList<>();
String[] split_result = new String[2];

split_result = "Audi SQ5,341".split(",");
cars2.add(split_result);

split_result = "LandRover Discovery,306".split(",");
cars2.add(split_result);

// Displaying the size of the list

System.out.println("The size of the linked list is: " + cars2.size());

// Displaying the Strings in the linkedlist ??? (not their addresses)

System.out.println("LinkedList:" + cars2);
cars2.forEach(element -> System.out.println(element));
  • 1
    Obviously you need two iterations, first over LinkedList the over each string's array – Selvin Dec 29 '20 at 17:32
  • @Spectric thanks Arrays.toString works well; how would one do that with 2 loops ? – Jake Otto Dec 29 '20 at 18:01
  • Does this answer your question? [What's the simplest way to print a Java array?](https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – QBrute Dec 29 '20 at 18:05
  • 1
    Generally we try to avoid generics with arrays. Why not use `List>` instead? Also its `toString()` method would generate more user-friendly result. – Pshemo Dec 29 '20 at 18:06
  • 3
    Also based on `[[Audi SQ5,341], [LandRover Discovery,306]]` you should be using `List` instead of `List`. Create your own `Car` class and let it hold any values you wish. Also don't forget to override its `toString()` method. – Pshemo Dec 29 '20 at 18:10

2 Answers2

1

I would use a Java Stream for that. It has the helpful joining collector:

LinkedList<String[]> cars2 = new LinkedList<>();

String output = cars2
  .stream()
  .map(Arrays::toString)
  .collect(Collectors.joining(", ", "[", "]"));
Thomas Preißler
  • 613
  • 4
  • 13
0

Your problem is that while default Java collections like LinkedList implement a nice toString to print their memebers, arrays do not. So either you have to iterate over your collection and print the results yourself, as some other answers suggest, or you have to put objects into your LinkedLists that have a nice toString implementation. Those can either be collections themselves List<List<String>>, or you can define a class to contain your data.

class CarInfo {
    // these should be encapsulated with getters/setters, I'm being lazy.
    public String model;
    public String value; // I don't know what this represents, or if it's a string or int

    public void toString() { return model + ", " + value; }
}

If you use Lombok, this is very easy with the @Data annotation, which will generate toString and getters/setters for you.

@Data
class CarInfo {
    String model;
    String value;
}

However, it won't be exactly the same as your desired output. If you want it specifically formatted with the brackets as you described, you'd have to do your own toString anyway.

ykaganovich
  • 14,736
  • 8
  • 59
  • 96