0

How to get or print String from ArrayList<String[]>?

ArrayList<String[]> arrayList = new ArrayList<>();
arrayList.add(new String[]{"A1","A1","A3"});
arrayList.add(new String[]{"B1","B1","B3"});
arrayList.add(new String[]{"C1","C1","C3"});

System.out.println("*** 1 ***");
System.out.println(arrayList);

System.out.println("*** 2 ***");
for (int i = 0; i < arrayList.size(); i++) {
    System.out.println((String[])arrayList.get(i));
}

System.out.println("*** 3 ***");
for (int i = 0; i < arrayList.size(); i++) {
    System.out.println(arrayList.get(i).toString());
}

System.out.println("*** 4 ***");
for (int i = 0; i < arrayList.size(); i++) {
    String[] strings = arrayList.get(i);
    System.out.println(strings);
}

output:

*** 1 ***
[[Ljava.lang.String;@2a85f3d6, [Ljava.lang.String;@404b7c69, [Ljava.lang.String;@1bd4f279]

*** 2 ***
[Ljava.lang.String;@2a85f3d6
[Ljava.lang.String;@404b7c69
[Ljava.lang.String;@1bd4f279
*** 3 ***
[Ljava.lang.String;@2a85f3d6
[Ljava.lang.String;@404b7c69
[Ljava.lang.String;@1bd4f279
*** 4 ***
[Ljava.lang.String;@2a85f3d6
[Ljava.lang.String;@404b7c69
[Ljava.lang.String;@1bd4f279
1
  • Perhaps you need two loops? Commented Jan 12, 2014 at 11:03

3 Answers 3

5

Sadly, java's default toString() for arrays is useless. You must use the utility method Arrays.toString(). This will work:

for (String[] strings : arrayList)
    System.out.println(Arrays.toString(strings));
Sign up to request clarification or add additional context in comments.

Comments

2

In Java, each object has toString() method, the default is displaying the class name representation, then adding @ and then the hashcode.

strings is an array of Strings. You should use Arrays#toString(), which is implemented this way (I advise you to go through it to better understand what's going on):

3860     public static String toString(int[] a) { {
3861        if (a == null)
3862            return "null";
3863        int iMax = a.length - 1;
3864        if (iMax == -1)
3865            return "[]";
3866
3867        StringBuilder b = new StringBuilder();
3868        b.append('[');
3869        for (int i = 0; ; i++) {
3870            b.append(a[i]);
3871            if (i == iMax)
3872                return b.append(']').toString();
3873            b.append(", ");
3874        }
3875    }

Or, you can loop on it and manually print the items:

for(String str: strings) {
    System.out.println(str + " ");
}

Comments

0

Try to modify this :

**@Override
public String toString() {
    return "abc [name=" + name + ", stage=" + stage
            + ", messageCode=" + messageCode + ", title=" + title
            + ", description=" + description + ", expiryTimeLeft="
            + expiryTimeLeft + ", holdLimit=" + holdLimit + ", points="
            + points];
}

Comments

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.