0

May this question is repeated but I tried most of the solution but none of them fit in my problem. Referring to this page : https://docs.oracle.com/javase/tutorial/uiswing/components/table.html

I want to create the JTable to read from objects from my custom class.As an initial step, I want to test the looping with the JTable. As follow:

Object[][] tripsData =new Object[3][6];
for(int j=0; j< 3;j++)
        for(int i =0; i< 6;i++)
        {   tripsData[j][i]= new Object[] {"Kathy", "Smith","wait",  "Snowboarding", 5, "false"};
        }

The code works fine except the output it shows like this:

 [Ljava.lang.Object;@41488024
 [Ljava.lang.Object;@54c83ae1
 [Ljava.lang.Object;@161a4f2c

I tried to use, toString() and valueOf(), but the same thing. Any suggestion to solve

5
  • 1
    [Ljava.lang.Object;@41488024 is the toString method of Object[] Commented May 8, 2018 at 13:55
  • @NengLiu so, how can I show the original text ?. I'm forced to use Object instead of String to put it in the JTable. Commented May 8, 2018 at 14:06
  • What's the expect result? Please edit the question. Commented May 8, 2018 at 14:09
  • And, do you really want to make tripsData an array? It is a single element in the matrix! Commented May 8, 2018 at 14:10
  • @NengLiu no it's not a single element. It contain dynamic data. Commented May 8, 2018 at 14:14

2 Answers 2

1

The problem is you are creating a three-dimensional array instead of the 2D that you need. In your code, you use

tripsData[j][i]= new Object[] {"Kathy", "Smith","wait",  "Snowboarding", 5, "false"};

thus "Kathy" in the resulting Object has an index [j][i][0], "Smith" has an index [j][i][1] and so on. Populate your array like so:

Object[][] tripsData =new Object[3][6];
        for(int j=0; j< 3;j++) {
            tripsData[j] = new Object[]{"Kathy", "Smith", "wait", "Snowboarding", 5, "false"};
        }

Then both this

    for (Object[] row : tripsData) {
        for (int j = 0; j < data[0].length; j++) {
            System.out.print(row[j]);
            System.out.print("\t");
        }
        System.out.print("\n");
    }

and this

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

will work fine

Sign up to request clarification or add additional context in comments.

2 Comments

THNAK YOU very much, yes you are right. As I put the whole columns in the object I shouldn't use it as a single cell.
Happy to help =)
0

You can use Arrays.toString(), somewhat similar to this snippet:

    Object[][] arr = new Object[2][2];
    arr[0][0] = "00";
    arr[0][1] = "01";
    arr[1][0] = "10";
    arr[1][1] = "11";
    System.out.println(Arrays.toString(arr[0]));
    System.out.println(Arrays.toString(arr[1]));

This will print

[00, 01]
[10, 11]

3 Comments

Thank you for your reply, but it doesn't work with me.
It doesn't work in what way? What output you see when you write System.out.println(tripsData[0]); after your example? Show us some code you tried, otherwise it's very difficult to help you.
Thank you for your support, It was solved by the next comment by evnica.

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.