2

i am trying to print arrayList but i am getting something like this.

[[Ljava.lang.String;@506c589e, [Ljava.lang.String;@69d0a921, [Ljava.lang.String;@446cdf90]

I also tried but no result.

Arrays.toString(list.get(0))

Please help.

public ArrayList getByName(String name) {
    try {
        preSt = con.prepareStatement("SELECT * FROM DATA WHERE name=?");
        preSt.setString(1, name);
        rs = preSt.executeQuery();
        ArrayList list = new ArrayList();
        int i = 0;
        while(rs.next()) {
            Object[] temp = new String[3];
            temp[0] = rs.getString("name");
            temp[1] = rs.getString("surname");
            temp[2] = rs.getString("age");
            list.add(i, temp);
            i++;
        }
        System.out.println(list);
        return list;
    } catch (Exception ex) {
        System.out.println("Error: "+ex);
    }
    return null;
}

3 Answers 3

5

Since you are already working with arrays, you may consider this method which will allow you to avoid writing a hand-coded loop:

System.out.println(Arrays.deepToString(list.toArray()));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this one works too. but i needed to print out arrays separately.
3

The easiest way to print that list is to iterate through it. Remember that each element of that list is itself an array, so you'll need Arrays.toString to print it.

List<Object[]> list = new ArrayList<>();

// populate it in here

for(Object[] array : list) {
    System.out.println(Arrays.toString(array));
}

2 Comments

I tried to run this code when list is returned here is a code which gets it. code public void getNew(List name) { for(Object[] array : name) { System.out.println(Arrays.toString(array)); } } i get error like this code Error:(15, 30) java: incompatible types: java.lang.Object cannot be converted to java.lang.Object[]
That error would only happen if you didn't change the declaration of list, like I've shown on the first line of code in my answer.
-1

You can try this

public ArrayList getByName(String name) {
    try {
        preSt = con.prepareStatement("SELECT * FROM DATA WHERE name=?");
        preSt.setString(1, name);
        rs = preSt.executeQuery();
        ArrayList list = new ArrayList();
        int i = 0;
        while(rs.next()) {
            Object[] temp = new String[3];
            temp[0] = rs.getString("name");
            temp[1] = rs.getString("surname");
            temp[2] = rs.getString("age");
            list.add(i, temp);
            i++;
        }
        for(int i=0;i<list.size();i++){
            System.out.println(list[i]);
        }
        return list;
    } catch (Exception ex) {
        System.out.println("Error: "+ex);
    }
    return null;
}

5 Comments

i tried this one and gor err like this Error:(63, 40) java: array required, but java.util.ArrayList<java.lang.Object> found
This won't fix the problem. Did you actually try it?
list is an ArrayList. You can't access elements using [] like an array.
Then u can you map instead of list.Because you are adding twom paramter so go with map like this
Map<int,object> map = new HashMap<int,object>();

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.