java - In my ArrayList of Arraylists, why are the all the actions on individual elements effecting all other indexes? -


i have nested arraylists compose 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 pagecontents = new pagecontents(); //create new line pagecontents.setline("this line on page"); //edit line book.get(0).add(pagecontents); //add new (first) line first page of book. 

so, in practice, book.get(0).get(0).getline(); return first line of of first page of book.

the problem

i thought working fine, since initial part of development involved getting single page right, worry multiple pages. then, when appended pagelist book arraylist book.new(page); found out same page @ book.get(0). value of book.get(0).get(0) same book.get(1).get(0), or book.get(0).get(20)was same book.get(1).get(20).

i thought "no big deal,it must copying data book.get(0). i'll clear new page arraylist book.get(1).clear(); right after create it." cleared book.get(0) too. changed data @ book.get(1).get(9);, , changed book.get(0).get(9); same value.

now don't know going on. thought might have few lines sync 'book' data different class, thinking perhaps copying pointers every`page' pointed same data, after of testing eliminated culprit. (i deleted methods)

is use of arraylists flawed in implementation?
far arraylist structure has been perfect purposes (until needed new page, is). need each new page arraylist blank , unique added book list.

any appreciated. guys!

more complete example:

code

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 new line pagecontents1.setline("this line 1 on page 1"); //edit line book.get(0).add(pagecontents1); //add new (first) line first page of 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 empty {     system.out.println("page2: empty"); } else {     system.out.println("page2: " + book.get(1).get(0).getline()); } 

output (incorrect)

page1 size: 1 page1: line 1 on page 1  page2 size: 1 page2: line 1 on page 1 

output (what should if code worked intended)

page1 size: 1 page1: line 1 on page 1  page2 size: 0 page2: empty    

if add new code (to give page 2 contents)

pagecontents pagecontents2 = new pagecontents(); //create new line pagecontents2.setline("this line 1 on page 2"); //edit line book.get(0).add(pagecontents1); //add new (first) line second page of 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()); 

output (incorrect, both pages making same changes)

page1 size: 2 page1: line 1 on page 1  page2 size: 2 page2: line 1 on page 1 page1 size: 2 page1: line 1 on page 2  page2 size: 2 page2: line 1 on page 2 

since added new line, both pagelists have 2 lines. each should have 1 line different values.

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 line 1 on page 1");          page1.add(pagecontents1);          book.add(page1);          arraylist<pagecontents> page2 = new arraylist<>();         pagecontents pagecontents2 = new pagecontents();         pagecontents2.setline("this 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());         }     } } 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -