Results 1 to 15 of 15
- 02-14-2015, 01:45 PM #1
Member
- Join Date
- Feb 2015
- Posts
- 12
- Rep Power
- 0
add to list with id, makes list upside down
hello,
I saved the content of an ArrayList<SomeClass> to a file
I write each serialised object to the file by adding to each an id that is created with list.indexOf(object) (and saved before each object)
so I can repopulated the list with SomeClass objects in the same order as they were
and once I reload the file, I do list.add(id,object)
well my list is populated upside down, I mean the first object is the last and the other way around
If i remove the id when I do the add(object), I dont have this problem
by curiosity, I wanted to know why, it does not make any sens to me so far
thanks
- 02-14-2015, 02:21 PM #2
Re: add to list with id, makes list upside down
What are the values of id that are used to load the ArrayList?
If you don't understand my response, don't ignore it, ask a question.
- 02-14-2015, 03:52 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: add to list with id, makes list upside down
Can you show us some of the (relevant) code?
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 02-14-2015, 06:01 PM #4
Member
- Join Date
- Feb 2015
- Posts
- 12
- Rep Power
- 0
Re: add to list with id, makes list upside down
here you go, I stripped out all objectdata save/load, left only the id part
Java Code:public void Save(String fileName) { DataOutputStream dos=null; FileOutputStream fos=null; try { fos = new FileOutputStream(fileName); dos = new DataOutputStream(fos); Iterator<NavigationCell> it = cells.iterator(); dos.writeInt(cells.size()); while(it.hasNext()) { NavigationCell nc=it.next(); int id=cells.indexOf(nc); <----------------------------------------------------------------------------------------- dos.write(id); ... } } catch(IOException ex){ System.out.println("NavMesh '"+fileName+"' could not be saved" ); } finally { try { if(fos!=null) fos.close(); if(dos!=null) dos.close(); } catch (IOException ex) { System.out.println("NavMesh '"+fileName+"', file could not be closed" ); } } System.out.println("NavMesh saved to '"+fileName+"'"); } public static NavigationMesh Load(String fileName) { FileInputStream is = null; DataInputStream dis=null; NavigationMesh out= null; try { is = new FileInputStream(fileName); // create new data input stream dis = new DataInputStream(is); int ncells =dis.readInt(); HashMap<NavigationCell, Integer[]> tempCellsLinks = new HashMap<NavigationCell,Integer[]>(); out= new NavigationMesh(); for(int i=0;i<ncells;i++) { int id=dis.read(); <----------------------------------------------------------------------------------------- ... out.AddCell(id,cell); } Iterator<NavigationCell> it = tempCellsLinks.keySet().iterator(); while(it.hasNext()) { NavigationCell cell = it.next(); Integer[] links=tempCellsLinks.get(cell); cell.setLinks(out.getCell(links[0]),out.getCell(links[1]),out.getCell(links[2])); } } catch (FileNotFoundException ex) { System.out.println("NavMesh '"+fileName+"', file could not be found" ); } catch (IOException ex) { System.out.println("NavMesh '"+fileName+"', something worng happened reading file" ); } finally { try { if(is!=null) is.close(); if(dis!=null) dis.close(); } catch (IOException ex) { Logger.getLogger(NavigationMesh.class.getName()).log(Level.SEVERE, null, ex); } } System.out.println("NavMesh file '"+fileName+"' read successfully"); return out; }
Java Code:private void AddCell(int id,NavigationCell cell) { cells.add(/*id,*/cell); }
- 02-14-2015, 06:04 PM #5
Member
- Join Date
- Feb 2015
- Posts
- 12
- Rep Power
- 0
Re: add to list with id, makes list upside down
well as I said, the list gets its order inverted
so if 1 2 3 4 5 , then 5 4 3 2 1
just as if I was doing list.add(0,data)Last edited by rompelstilchen; 02-14-2015 at 06:07 PM.
- 02-14-2015, 06:10 PM #6
Member
- Join Date
- Feb 2015
- Posts
- 12
- Rep Power
- 0
Re: add to list with id, makes list upside down
ok I figured out write() saves byte instead of integer, that was the issue
my badLast edited by rompelstilchen; 02-14-2015 at 06:15 PM.
- 02-14-2015, 06:10 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: add to list with id, makes list upside down
It's time to use the poor man's debugger: print out the ids when you save them and print out the ids when you load them again; there must be something fishy going on ...
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 02-14-2015, 06:28 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 02-14-2015, 06:33 PM #9
Re: add to list with id, makes list upside down
Is that the writeInt() method vs the write() method?
If you don't understand my response, don't ignore it, ask a question.
- 02-14-2015, 06:46 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 02-14-2015, 06:48 PM #11
Re: add to list with id, makes list upside down
Have one for me.
If you don't understand my response, don't ignore it, ask a question.
- 02-14-2015, 07:14 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: add to list with id, makes list upside down
Consider it done.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 02-14-2015, 08:43 PM #13
Re: add to list with id, makes list upside down
Why did I have to read this when I'm out of beer?
d[ry]bIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 02-15-2015, 07:33 AM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: add to list with id, makes list upside down
Rule #1: never, ever, run out of beer; my liquor store owner is a very nice man: he refills my supply of Grolsch every week; he always puts new a crate in my barn so I never have to be afraid to run out of anything ...
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 02-15-2015, 12:05 PM #15
Member
- Join Date
- Feb 2015
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
[Adding with generics]List <? super Cat> list = new ArrayList<Animal>()
By garyiskidding in forum New To JavaReplies: 4Last Post: 05-21-2013, 10:21 AM -
read emailid and seperate print names in a list and domain in another list
By jellora in forum New To JavaReplies: 1Last Post: 03-25-2012, 10:25 PM -
Linked List, Array List time complexity
By Rick99771977 in forum New To JavaReplies: 4Last Post: 08-18-2011, 06:37 AM -
How do I list values of an arrays in a comma seperted list
By nmvictor in forum New To JavaReplies: 2Last Post: 11-22-2009, 06:24 PM -
How do I list values of an arrays in a comma seperted list
By nmvictor in forum New To JavaReplies: 3Last Post: 11-21-2009, 06:48 PM
Bookmarks