简体   繁体   English

JTable二维对象数组

[英]JTable two-dimensional object array

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 引用此页面: 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. 我想创建一个JTable来读取自定义类中的对象。作为第一步,我想测试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. 我尝试使用toString()和valueOf(),但是还是一样。 Any suggestion to solve 任何解决的建议

The problem is you are creating a three-dimensional array instead of the 2D that you need. 问题是您正在创建三维数组,而不是所需的2D。 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. 因此,结果Object中的"Kathy"具有索引[j][i][0]"Smith"具有索引[j][i][1] ,依此类推。 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 会很好的工作

You can use Arrays.toString() , somewhat similar to this snippet: 您可以使用Arrays.toString() ,此代码段与此类似:

    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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM