在我的Arraylists的ArrayList中,为什么单个元素上的所有操作都会影响所有其他索引?

时间:2016-11-17 16:35:16

标签: java oop object arraylist javafx

我已经嵌套了构成一本书的Arraylists:

private ArrayList<ArrayList<PageContents>> book = new ArrayList<>(); //A list of lists
private ArrayList<PageContents> page = new ArrayList<>(); //A List of objects

book.add(page); //Add new(first) page in book.
PageContents pageContents = new PageContents(); //Create a new line
pagecontents.setLine("This is a line on the page"); //Edit the line
book.get(0).add(pagecontents); //Add the new (first) line to the first page of the book.

因此,在实践中,book.get(0).get(0).getLine();将返回本书第一页的第一行。

问题

我最初认为一切正常,因为我的开发的最初部分涉及正确的单页,然后我会担心多个页面。然后,当我使用pagebook列表附加到book.new(page); ArrayList时,我发现它与page处的book.get(0)完全相同。 因此book.get(0).get(0)的值与book.get(1).get(0)完全相同,或book.get(0).get(20)book.get(1).get(20)完全相同。

然后我想&#34;没什么大不了的,它必须从book.get(0)复制数据。我创建它之后立即用page清除新的book.get(1).clear(); arraylist。&#34;但那也清除了book.get(0)。然后我更改了book.get(1).get(9);处的数据,并将book.get(0).get(9);更改为相同的值。

现在我不知道发生了什么。我认为这可能与我在同步“预订”的几行有关。不同类别的数据,也许是复制指针所以每个页面都是如此。他指出了同样的数据,但经过一次测试后,我把它作为罪魁祸首。 (我刚刚删除了这些方法)

我对ArrayLists的使用在我的实现中是否存在缺陷? 到目前为止,ArrayList结构非常适合我的目的(直到我需要一个新的page,即)。我只需要将每个新的page arraylist设为空白且唯一添加到book列表的内容。

感谢任何帮助。谢谢伙计们!

更完整的示例:

代码

private ArrayList<ArrayList<PageContents>> book = new ArrayList<>(); //A list of lists
private ArrayList<PageContents> page = new ArrayList<>(); //A List of objects

book.add(page); //Add new(first) page in book.
PageContents pageContents1 = new PageContents(); //Create a new line
pagecontents1.setLine("This is Line 1 on Page 1"); //Edit the line
book.get(0).add(pagecontents1); //Add the new (first) line to the first page of the book.

System.out.println("Page1 size: " + book.get(0).size() + "pages");
System.out.println("Page1: " + book.get(0).get(0).getLine());

System.out.println("Page2 size: " + book.get(1).size() + "pages");

if(book.get(1).isEmpty())  //Should book.get(1) should be empty
{
    System.out.println("Page2: EMPTY");
}
else
{
    System.out.println("Page2: " + book.get(1).get(0).getLine());
}

输出(不正确)

Page1 size: 1
Page1: This is Line 1 on Page 1 
Page2 size: 1
Page2: This is Line 1 on Page 1

输出(如果代码按预期工作,应该是什么)

Page1 size: 1
Page1: This is Line 1 on Page 1 
Page2 size: 0
Page2: EMPTY   

如果我添加此新代码(提供第2页内容)

PageContents pageContents2 = new PageContents(); //Create a new line
pagecontents2.setLine("This is Line 1 on Page 2"); //Edit the line
book.get(0).add(pagecontents1); //Add the new (first) line to the second page of the book.

System.out.println("Page1 size: " + book.get(0).size() + "pages");
System.out.println("Page1: " + book.get(0).get(1).getLine());
System.out.println("Page2 size: " + book.get(1).size() + "pages");
System.out.println("Page2: " + book.get(1).get(1).getLine());

输出(不正确,两个页面进行相同的更改)

Page1 size: 2
Page1: This is Line 1 on Page 1 
Page2 size: 2
Page2: This is Line 1 on Page 1
Page1 size: 2
Page1: This is Line 1 on Page 2 
Page2 size: 2
Page2: This is Line 1 on Page 2

由于我添加了一个新行,因此page个列表都有2行。每个应该只有一行具有不同的值。

2 个答案:

答案 0 :(得分:2)

你确定你的演示程序是正确的吗?

我尝试了下面的代码,得到了一个ArrayIndexOutOfBounds异常。你的代码必须做的比你发布的更多......

Exception in thread "main" Page1 size: 1pages
Page1: This is Line 1 on Page 1
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at BookThingy.main(BookThingy.java:17)

...代码

import java.util.ArrayList;

public class BookThingy {

    public static void main(String[] args) {
        ArrayList<ArrayList<PageContents>> book = new ArrayList<>(); //A list of lists
        ArrayList<PageContents> page = new ArrayList<>(); //A List of objects

        book.add(page); //Add new(first) page in book.
        PageContents pageContents1 = new PageContents(); //Create a new line
        pageContents1.setLine("This is Line 1 on Page 1"); //Edit the line
        book.get(0).add(pageContents1); //Add the new (first) line to the first page of the book.

        System.out.println("Page1 size: " + book.get(0).size() + "pages");
        System.out.println("Page1: " + book.get(0).get(0).getLine());

        System.out.println("Page2 size: " + book.get(1).size() + "pages");

        if(book.get(1).isEmpty())  //Should book.get(1) should be empty
        {
            System.out.println("Page2: EMPTY");
        }
        else
        {
            System.out.println("Page2: " + book.get(1).get(0).getLine());
        }
    }

    public static class PageContents {
        private String line;

        public String getLine() {
            return line;
        }

        public void setLine(String line) {
            this.line = line;
        }
    }
}

发布您的完整代码(如果可能)或检查其他方法是否未修改您的列表,然后我将修改此答案...

答案 1 :(得分:2)

import java.util.ArrayList;

class PageContents
{
    String Line;

    public String getLine()
    {
        return Line;
    }

    public void setLine(String line)
    {
        Line = line;
    }

}

public class MainClass
{
    public static void main(String[] args)
    {
        ArrayList<ArrayList<PageContents>> book = new ArrayList<>(); 
        ArrayList<PageContents> page1 = new ArrayList<>();

        // Add new(first) page in book.
        PageContents pageContents1 = new PageContents();
        pageContents1.setLine("This is Line 1 on Page 1"); 
        page1.add(pageContents1); 
        book.add(page1);

        ArrayList<PageContents> page2 = new ArrayList<>();
        PageContents pageContents2 = new PageContents();
        pageContents2.setLine("This is Line 1 on Page 2");
        page2.add(pageContents2);
        book.add(page2);

        System.out.println("Page1 size: " + book.get(0).size() + "pages");
        System.out.println("Page1: " + book.get(0).get(0).getLine());

        System.out.println("Page2 size: " + book.get(1).size() + "pages");

        if (book.get(1).isEmpty())
        {
            System.out.println("Page2: EMPTY");
        } else {
            System.out.println("Page2: " + book.get(1).get(0).getLine());
        }
    }
}