Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用对象列表使用JTable中定义的起始位置设置值?_Java_Arraylist - Fatal编程技术网

Java 如何使用对象列表使用JTable中定义的起始位置设置值?

Java 如何使用对象列表使用JTable中定义的起始位置设置值?,java,arraylist,Java,Arraylist,我有一个要放在JTable中的对象列表。下面的映射以索引作为键,数组作为值。整数数组的第一个元素是行号,第二个元素是列号: mStartLocationMap = new HashMap<Integer, int[]>(); // private static mStartLocationMap.put(1,new int[]{0,0}); mStartLocationMap.put(2,new int[]{1,0}); mStartLocationM

我有一个要放在JTable中的对象列表。下面的映射以索引作为键,数组作为值。整数数组的第一个元素是行号,第二个元素是列号:

    mStartLocationMap = new HashMap<Integer, int[]>(); // private static
    mStartLocationMap.put(1,new int[]{0,0});
    mStartLocationMap.put(2,new int[]{1,0});
    mStartLocationMap.put(3,new int[]{0,1});
    mStartLocationMap.put(4,new int[]{1,1});
mStartLocationMap=newhashmap();//私人静电
mStartLocationMap.put(1,新的int[]{0,0});
mStartLocationMap.put(2,新的int[]{1,0});
mStartLocationMap.put(3,新的int[]{0,1});
mStartLocationMap.put(4,新int[]{1,1});
根据我得到的索引,我检索适当的起始位置。该表为16行X 24列。但是,逻辑是这样的,无论起始位置是什么,我拥有的对象的arraylist,每个对象都必须放置在其他每个单元格中,直到我到达列的末尾。然后,这些行也每隔一行递增一次。以下是最终产品的外观(这是在我的列表中有96个元素的情况下完成的,代码如下所示:

这是我的密码:

    // rowStart and colStart can be 0 or 1
    int counter = 0;
    for(int row=rowStart; row<rowCount ;row=row+2)    // rowcount =16
    {
        for(int col=colStart; col<colCount; col=col+2) // colcount=24
        {
            // myObjs is an ArrayList that contains 96 or less elements. This line throws the exception
            MyObject temp = myObjs.get(counter); 

            myTable.setValueAt(temp,row,col);
            counter++;
        }
    }
//rowStart和colStart可以是0或1
int计数器=0;

对于(int row=rowStart;row您可能会在此行遇到异常:

MyObject temp = myObjs.get(counter);

因为您的数组列表将是空的,并且您正在尝试访问第0个元素。

因此,如果您的数组列表小于96,那么您想要的是:

┌─┬─┬─┬─┬─┬─┬─┬─┐
│*│ │*│ │*│ │*│ │
├─┼─┼─┼─┼─┼─┼─┼─┤
│ │ │ │ │ │ │ │ │
├─┼─┼─┼─┼─┼─┼─┼─┤
│*│ │*│ │ │ │ │ │
├─┼─┼─┼─┼─┼─┼─┼─┤
│ │ │ │ │ │ │ │ │
├─┼─┼─┼─┼─┼─┼─┼─┤
│ │ │ │ │ │ │ │ │
└─┴─┴─┴─┴─┴─┴─┴─┘
在这种情况下,您可以循环使用列表,并调整当前行和列,而不是相反:

int row=rowStart;
int col=colStart;
用于(MyObject温度:MyObject){
myTable.setValueAt(温度、行、列);
col+=2;
如果(列>=列计数){
col=colStart;
行+=2;
}
}
基本上,从
rowStart
colStart
开始。对于列表中的每个值,将其添加到表中,然后计算下一个位置。首先,移动到下一列。如果发现列数已超过,则需要转到下一行。再次将列调整为第一列n、 并更新您的行


您不需要检查行的限制,因为列表中的元素不应该多于表中的元素。

MyObj的类型是什么?如果是数组列表,那么MyObj可以容纳多少元素?这是一个自定义对象。但是,MyObj是一个ArrayList。忘记包含
计数器+
-请参阅更新。有多少个el元素在列表中?可以是任意数量的元素抱歉,我不清楚。我知道哪一行会抛出错误-在尝试了一段时间后,我不知道如何实现我想要的。是的,这肯定更有意义。我已经更新了我的代码,它工作得非常完美。我想我太担心位置了。谢谢你的帮助!疯狂的是,我开始做你做过的事情,但没有进一步试验就立即放弃,然后继续做相反的事情:/…学到的教训!