Results 1 to 15 of 15
  1. #1
    Join Date
    Feb 2015
    Posts
    12
    Rep Power
    0

    Default 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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: add to list with id, makes list upside down

    Can you show us some of the (relevant) code?

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    Join Date
    Feb 2015
    Posts
    12
    Rep Power
    0

    Default 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); 
        }

  5. #5
    Join Date
    Feb 2015
    Posts
    12
    Rep Power
    0

    Default 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 05:07 PM.

  6. #6
    Join Date
    Feb 2015
    Posts
    12
    Rep Power
    0

    Default 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 bad
    Last edited by rompelstilchen; 02-14-2015 at 05:15 PM.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default 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,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: add to list with id, makes list upside down

    Quote Originally Posted by rompelstilchen View Post
    ok I figured out write() saves byte instead of integer, that was the issue
    my bad
    But you'e using DataOutput/InputStreams; they write and read 4 byte integers ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: add to list with id, makes list upside down

    Quote Originally Posted by Norm View Post
    Is that the writeInt() method vs the write() method?
    Grmbl, I missed that one completely; I'm going to drink a Grolsch beer ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: add to list with id, makes list upside down

    Consider it done.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default 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]b
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default 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,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  15. #15
    Join Date
    Feb 2015
    Posts
    12
    Rep Power
    0

    Default Re: add to list with id, makes list upside down

    try Chimay blue, my favourite, but dont know if its available everywhere :-)

Similar Threads

  1. Replies: 4
    Last Post: 05-21-2013, 09:21 AM
  2. Replies: 1
    Last Post: 03-25-2012, 09:25 PM
  3. Linked List, Array List time complexity
    By Rick99771977 in forum New To Java
    Replies: 4
    Last Post: 08-18-2011, 05:37 AM
  4. Replies: 2
    Last Post: 11-22-2009, 05:24 PM
  5. Replies: 3
    Last Post: 11-21-2009, 05:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •