它只在链表中插入最后一个矩阵

阿尤布·埃洛马里

我想在 LinkedList 中插入许多充满许多对象的矩阵,例如EmptyOther但它只插入最后一个矩阵,请帮忙

public LinkedList<Object[][]> addMatrices()
{
    LinkedList<Object[][]> l=new LinkedList<>();
    Object[][] o=new Object[2][2];
    o[0][0]=new Empty(2);
    o[0][1]=new Other();    
    o[1][0]=new Empty(4);
    o[1][1]=new Empty(6);
    l.add(o);
    o[0][0]=new Empty(4);
    o[0][1]=new Other();    
    o[1][0]=new Empty(5);
    o[1][1]=new Empty(1);
    l.add(o);
    for(Object[][] oo:l)
    {

        for(int x=0;x<oo.length;x++){
            for(int y=0;y<oo[x].length;y++)
                {System.out.print("\t"+oo[x][y]+" ");
            System.out.print("\t|");}
            System.out.println(System.lineSeparator());
        }
        System.out.println(System.lineSeparator());
    }
    return l;
}

输出:

4   |   -1  |

5   |   1   |


4   |   -1  |

5   |   1   |

它应该是这样的:

2   |   -1  |

4   |   6   |


4   |   -1  |

5   |   1   |
猞猁242

发生这种情况是因为您的对象 o 是通过引用 LinkedList l 添加的。所以你实际上覆盖了第一个添加的对象,看起来好像只添加了最后一个。

试试这个方法:

public LinkedList<Object[][]> addMatrices()
{
    LinkedList<Object[][]> l=new LinkedList<>();
    Object[][] o=new Object[2][2];
    o[0][0]=new Empty(2);
    o[0][1]=new Other();    
    o[1][0]=new Empty(4);
    o[1][1]=new Empty(6);
    l.add(o);

    Object[][] o2=new Object[2][2];
    o2[0][0]=new Empty(4);
    o2[0][1]=new Other();    
    o2[1][0]=new Empty(5);
    o2[1][1]=new Empty(1);
    l.add(o2);

    for(Object[][] oo:l)
    {

       for(int x=0;x<oo.length;x++){
           for(int y=0;y<oo[x].length;y++) {
               System.out.print("\t"+oo[x][y]+" ");
               System.out.print("\t|");}
               System.out.println(System.lineSeparator());
           }
           System.out.println(System.lineSeparator());
       }
    return l;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

插入链表中总是返回最后一个节点,而不是当前节点

链表在排序链表中插入一个节点

删除链表中的最后一个节点

从链表C#中删除最后一个节点

查找单链表中的最后一个 K

最后一个节点未打印在链表中

在一个循环中,innerHTML 只插入最后一个被调用的元素

在foreach循环中遍历laravel的结果时,它只保存新数组元素中的最后一个元素?

为什么它只更改列表中的最后一个字符串?(for循环)python

在c的链表中插入一个条目

如何只从矩阵中获取一个连接

只覆盖函数中的最后一个默认参数

只返回ArrayList中的最后一个元素

XLSX 只写入数组中的最后一个索引

Dispatcher 只从数组中绘制最后一个对象

如何只选择表中的最后一个条目?

R 中的 for 循环只迭代最后一个条目

.map() 只返回数组中的最后一个对象

For 循环只迭代列表中的最后一个元素

python风险游戏,我无法让它只比较最后一个的所有数字

等待最后一个异步调用和只返回它之间有区别吗?

当我打印动态分配的数组时,它只打印最后一个值

如何从双向链表中删除第一个和最后一个节点?

插入一个简单的单链表

矩阵中的最后一个数字是错误的

改变一个矩阵,以便在 Python 中只更改列表而不是整个矩阵

仅将最后一个值插入Coredata中

获取插入到集合中的最后一个值

如何获取linq中的最后一个插入ID