compare two ArrayLists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • itsraghz
    New Member
    • Mar 2007
    • 124

    #16
    Hi,

    If my understanding on your requirement is right, you want to have a program which will read the inputs (say the different data - id, boolean,data) and store them into an ArrayList. For every run [assume, as of now your requirement is only 2 times], the program should compare the individual values of the present run with that of the previously entered values.

    In order to get the previously entered values, we should store the data somewhere so that we can fetch it later. Since the control/scope is lost once the program execution finishes, we can't store the data with any variables. The only way is to store it to any permanent storage medium so that you can get it back. Thats why the SERIALIZATION comes in - the easy way of managing things.

    You can think Serialization as equal to store it into the database but with the different arena. {thats not our scope now, i assume you are comfortable with serialization by now}.

    Now, the main part is, for every time you run, you first get the values from the user and populate your arraylist object with these values. Then fetch the previous values and compare the individual attributes/values.

    What you can do is,

    (a) Keep a counter variable which you append it to the end of the file.
    You retrieve this value and increment it for the next file to be stored.But you should store the counter variable also in another file !!

    (b) You can append the current datetime value with the filename while storing it. No matter how many files are present, you just keep writing a new file everytime. While retrieving, you just instantiate different arraylist objects as that of number of files in the directory. Then you can compare the values.

    Hope this helps....

    Comment

    • shana07
      Contributor
      • Jan 2007
      • 280

      #17
      Originally posted by itsraghz
      Hi,

      If my understanding on your requirement is right, you want to have a program which will read the inputs (say the different data - id, boolean,data) and store them into an ArrayList. For every run [assume, as of now your requirement is only 2 times], the program should compare the individual values of the present run with that of the previously entered values.

      In order to get the previously entered values, we should store the data somewhere so that we can fetch it later. Since the control/scope is lost once the program execution finishes, we can't store the data with any variables. The only way is to store it to any permanent storage medium so that you can get it back. Thats why the SERIALIZATION comes in - the easy way of managing things.

      You can think Serialization as equal to store it into the database but with the different arena. {thats not our scope now, i assume you are comfortable with serialization by now}.

      Now, the main part is, for every time you run, you first get the values from the user and populate your arraylist object with these values. Then fetch the previous values and compare the individual attributes/values.

      What you can do is,

      (a) Keep a counter variable which you append it to the end of the file.
      You retrieve this value and increment it for the next file to be stored.But you should store the counter variable also in another file !!

      (b) You can append the current datetime value with the filename while storing it. No matter how many files are present, you just keep writing a new file everytime. While retrieving, you just instantiate different arraylist objects as that of number of files in the directory. Then you can compare the values.

      Hope this helps....
      Thank you friend. Your understanding about my aim is absolutely RIGHT.
      After reading your notes I now know what is SERIALIZATION for (at least).
      I would say that I need to proper digest your advice on how to implement it in my program...

      Just one thing I don't get here is counter variable. If I am not mistaken, can I say that counter variable here is same as ArrayList size (in my case is oneList.size()) ?

      Or I do have to create/generate a new variable as counter?Here I have class Entry (id, boolean,data).. ...

      Thanks again....I shall try it first and see how it goes..

      Comment

      • itsraghz
        New Member
        • Mar 2007
        • 124

        #18
        Hi,

        You are correct. The counter i meant to say was what you generate to keep track of the files.. So inorder to create the new value which is incremental of the previous value, you need the previous value. Thats why i told you should store the counter value also in a separate file.

        Say, for the first time, you store the arraylist contents through Serialization in the file named "ArrayListConte nts-1.txt". The next iteration should store it in "ArrayListConte nts-2.txt". Right? For generating the 2, you need the previous value 1. Where would you take it? You should have stored it somewhere else and retrieve it now for incrementing it to be used this time.

        Hope this clears..
        Good luck.

        Comment

        • shana07
          Contributor
          • Jan 2007
          • 280

          #19
          Originally posted by itsraghz
          Hi,

          You are correct. The counter i meant to say was what you generate to keep track of the files.. So inorder to create the new value which is incremental of the previous value, you need the previous value. Thats why i told you should store the counter value also in a separate file.

          Say, for the first time, you store the arraylist contents through Serialization in the file named "ArrayListConte nts-1.txt". The next iteration should store it in "ArrayListConte nts-2.txt". Right? For generating the 2, you need the previous value 1. Where would you take it? You should have stored it somewhere else and retrieve it now for incrementing it to be used this time.

          Hope this clears..
          Good luck.
          Phew! I feel dizzy doing this assignment, really :)

          Now, I experience what you meant..It's complicated in second execution.
          First part is done fairly easy (after you guys helped me), now I have problem on how to retrieve the ArrayListConten ts-2.txt.

          At first I tried to make a loop from the current Entry size and retrieve the first output but having CONFLICT with the first Entry size that also has same variable name. oh god sorry if I make you confuse....I don't how to explain...If possible please take a look at my codes below:
          Code:
          listOne.add(new Entry(uniqueID, data, booleanValue));  
          try
                  {
                      FileOutputStream fout  = new FileOutputStream (FILENAME, true); 
                      ObjectOutputStream oos = new ObjectOutputStream( fout );            
                      oos.writeObject(uniqueID); 
                      oos.writeObject(data);
                      oos.writeObject(booleanValue);
                      
                      oos.flush(); 
                      fout.close();          
          
                      FileInputStream fis = new FileInputStream(FILENAME);
                      ObjectInputStream ois = new ObjectInputStream(fis); 
                      Object obj = ois.readObject();
                                   
                      List<Entry> listFromFile = (ArrayList) obj;
          	    for (Entry listOne: listFromFile) 
                      {
          	try
                          {
                              FileInputStream fis1 = new FileInputStream("output1.txt");  //DO I HAVE TO CREATE NEW FIS FOR READING THE FIRST OUTPUT FILE?
                              ObjectInputStream ois1 = new ObjectInputStream(fis1); 
                              Object obj1 = ois1.readObject();
                          
                             for (int i = 0; i <listOne.size(); i++)  //HERE IS THE CONFLICT!! -  SAME ARRAYLIST NAME CREATED
                             {  
                               Entry myEnt = (Entry)listOne.get(i);
          
                               if(myEnt.equalsId((Entry)listOne.get(i)))
                               {
                                  if(myEnt.equalsData((Entry)listOne.get(i)))
                                  {
                                   if(myEnt.equals((Entry)listOne.get(i))) 
                                   { 
                                       System.out.println ("1");
                                         
                                   }
                                  else 
                                  {
                                    System.out.println ("2");                  
                                  } 
                               }
                               else
                                  {
                                    System.out.println ("3");
                                    
                                   } 
                               }
                             }    
                          }
                       
                     catch (FileNotFoundException e) 
                    {
                         e.printStackTrace(); 
                    }
                     catch (IOException e) 
                     {
                        System.out.println("There was a problem creating/writing to the temp file");
                        e.printStackTrace();
                     } 
                     catch (ClassNotFoundException e) 
                     {
                          e.printStackTrace();
                     } 
                   }
          
                    }
                     catch (FileNotFoundException e) 
                    {
                         e.printStackTrace(); 
                    }
                     catch (IOException e) 
                     {
                        System.out.println("There was a problem creating/writing to the temp file");
                        e.printStackTrace();
                     } 
                     catch (ClassNotFoundException e) 
                     {
                          e.printStackTrace();
                     } 
          
            }

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #20
            Originally posted by shana07
            Phew! I feel dizzy doing this assignment, really :)

            Now, I experience what you meant..It's complicated in second execution.
            First part is done fairly easy (after you guys helped me), now I have problem on how to retrieve the ArrayListConten ts-2.txt.

            At first I tried to make a loop from the current Entry size and retrieve the first output but having CONFLICT with the first Entry size that also has same variable name. oh god sorry if I make you confuse....I don't how to explain...If possible please take a look at my codes below:
            Code:
            listOne.add(new Entry(uniqueID, data, booleanValue)); 
            try
            {
            FileOutputStream fout = new FileOutputStream (FILENAME, true); 
            ObjectOutputStream oos = new ObjectOutputStream( fout ); 
            oos.writeObject(uniqueID); 
            oos.writeObject(data);
            oos.writeObject(booleanValue);
             
            oos.flush(); 
            fout.close(); 
             
            FileInputStream fis = new FileInputStream(FILENAME);
            ObjectInputStream ois = new ObjectInputStream(fis); 
            Object obj = ois.readObject();
             
            List<Entry> listFromFile = (ArrayList) obj;
            	 for (Entry listOne: listFromFile) 
            {
            	try
            {
            FileInputStream fis1 = new FileInputStream("output1.txt"); //DO I HAVE TO CREATE NEW FIS FOR READING THE FIRST OUTPUT FILE?
            ObjectInputStream ois1 = new ObjectInputStream(fis1); 
            Object obj1 = ois1.readObject();
             
            for (int i = 0; i <listOne.size(); i++) //HERE IS THE CONFLICT!! - SAME ARRAYLIST NAME CREATED
            { 
            Entry myEnt = (Entry)listOne.get(i);
             
            if(myEnt.equalsId((Entry)listOne.get(i)))
            {
            if(myEnt.equalsData((Entry)listOne.get(i)))
            {
            if(myEnt.equals((Entry)listOne.get(i))) 
            { 
            System.out.println ("1");
             
            }
            else 
            {
            System.out.println ("2"); 
            } 
            }
            else
            {
            System.out.println ("3");
             
            } 
            }
            } 
            }
             
            catch (FileNotFoundException e) 
            {
            e.printStackTrace(); 
            }
            catch (IOException e) 
            {
            System.out.println("There was a problem creating/writing to the temp file");
            e.printStackTrace();
            } 
            catch (ClassNotFoundException e) 
            {
            e.printStackTrace();
            } 
            }
             
            }
            catch (FileNotFoundException e) 
            {
            e.printStackTrace(); 
            }
            catch (IOException e) 
            {
            System.out.println("There was a problem creating/writing to the temp file");
            e.printStackTrace();
            } 
            catch (ClassNotFoundException e) 
            {
            e.printStackTrace();
            } 
             
            }

            You know you can just use one text file and one ArrayList of ArrayLists.

            You always use that ArrayList to add new ArrayLists and so to the file you are working with only one ArrayList and it becomes easier to store and get.

            Comment

            • itsraghz
              New Member
              • Mar 2007
              • 124

              #21
              Yes thats right.. you could use a single file and a collection of ArrayLists in case you need to go for more and repeated runs.

              Comment

              • shana07
                Contributor
                • Jan 2007
                • 280

                #22
                Originally posted by itsraghz
                Yes thats right.. you could use a single file and a collection of ArrayLists in case you need to go for more and repeated runs.
                But friends, how to do that..
                Since the first execution will produce one ArrayList of Entry (MANY entries) with 3 attributes (for each entry) in a text file, then for the second execution, if I just use the same text file don't you think that it will replace the first output?

                But then you're definitely right to just use a single file and collection of arraylists because I need to go for many loops in order to compare the attributes...bu t how?
                Please guide me...........th anks a lot

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #23
                  Originally posted by shana07
                  But friends, how to do that..
                  Since the first execution will produce one ArrayList of Entry (MANY entries) with 3 attributes (for each entry) in a text file, then for the second execution, if I just use the same text file don't you think that it will replace the first output?

                  But then you're definitely right to just use a single file and collection of arraylists because I need to go for many loops in order to compare the attributes...bu t how?
                  Please guide me...........th anks a lot
                  Before you write again to the ArrayList you first retrieve the one previously written to the file. then you add the new ArrayList to that ArrayList and write back the ArrayList of ArrayLists.

                  Comment

                  • shana07
                    Contributor
                    • Jan 2007
                    • 280

                    #24
                    Originally posted by r035198x
                    Before you write again to the ArrayList you first retrieve the one previously written to the file. then you add the new ArrayList to that ArrayList and write back the ArrayList of ArrayLists.
                    I'm still working on it..by the way, friend of mine once said that I maybe need to change to FileReader/FileWriter in order to for me to save each attribute in a single line....and read the file line by line...
                    grr...clueless. .

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #25
                      Originally posted by shana07
                      I'm still working on it..by the way, friend of mine once said that I maybe need to change to FileReader/FileWriter in order to for me to save each attribute in a single line....and read the file line by line...
                      grr...clueless. .
                      No. don't write line by line. You'd have to break up the ArrayList into into it's contents first and regroup them again when you read them. Just write one arraylist object to the file using serialization and if you need to add another arraylist then just add it as an element of that arraylist.

                      Comment

                      • shana07
                        Contributor
                        • Jan 2007
                        • 280

                        #26
                        Originally posted by r035198x
                        No. don't write line by line. You'd have to break up the ArrayList into into it's contents first and regroup them again when you read them. Just write one arraylist object to the file using serialization and if you need to add another arraylist then just add it as an element of that arraylist.
                        Ok. The first execution output file written in per entry as one ArrayList variable - listOne.
                        Then for second time I don't know how to read them..what's the code to readObject:
                        this is my codes but I think it's wrong...
                        Code:
                         List<Entry> listOne = new ArrayList<Entry>();
                        ....
                        try
                                {
                                     /* RETRIEVE THE ONE PREVIOUSLY WRITTEN TO THE FILE */
                                    FileInputStream fis = new FileInputStream(FILENAME);
                                    ObjectInputStream ois = new ObjectInputStream(fis); 
                                    
                                    Object position = (listOne)ois.readObject(); //HERE- HOW TO RETRIEVE THEM AS THERE ARE 3 ATTRIBUTES THAT I HAVE TO COMPARE
                                    Object number =  (listOne)ois.readObject();
                                    value = ....

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #27
                          Originally posted by shana07
                          Ok. The first execution output file written in per entry as one ArrayList variable - listOne.
                          Then for second time I don't know how to read them..what's the code to readObject:
                          this is my codes but I think it's wrong...
                          Code:
                          List<Entry> listOne = new ArrayList<Entry>();
                          ....
                          try
                          {
                          /* RETRIEVE THE ONE PREVIOUSLY WRITTEN TO THE FILE */
                          FileInputStream fis = new FileInputStream(FILENAME);
                          ObjectInputStream ois = new ObjectInputStream(fis); 
                           
                          Object position = (listOne)ois.readObject(); //HERE- HOW TO RETRIEVE THEM AS THERE ARE 3 ATTRIBUTES THAT I HAVE TO COMPARE
                          Object number = (listOne)ois.readObject();
                          value = ....

                          Where is the ArrayList of ArrayLists?

                          You are supposed to have something like

                          Code:
                           ArrayList lists = new ArrayList(); 
                           
                          ArrayList listOne = new ArrayList();
                          //add stuff to listOne
                          lists.add(listOne);
                          then write lists to the file and save it there.
                          In program 2 get lists (using readObject) from the file.
                          get listOne using
                          listOne = lists.get(0);
                          If you want to save another arraylist in program 2 then simply to
                          lists.add(listT wo);
                          And you now have two arraylists inside lists. You can then overwrite the previous lists with this new one e.t.c

                          Comment

                          • shana07
                            Contributor
                            • Jan 2007
                            • 280

                            #28
                            Originally posted by r035198x
                            Where is the ArrayList of ArrayLists?

                            You are supposed to have something like

                            Code:
                             ArrayList lists = new ArrayList(); 
                             
                            ArrayList listOne = new ArrayList();
                            //add stuff to listOne
                            lists.add(listOne);
                            then write lists to the file and save it there.
                            In program 2 get lists (using readObject) from the file.
                            get listOne using
                            listOne = lists.get(0);
                            If you want to save another arraylist in program 2 then simply to
                            lists.add(listT wo);
                            And you now have two arraylists inside lists. You can then overwrite the previous lists with this new one e.t.c
                            I don't have ArrayList of ArrayLists (I think).. I only have
                            Code:
                            List<Entry>listOne = new ArrayList<Entry>();
                            Because of I have class Entry which I think is the same as what you mentioned ArrayList in here. I don't know. maybe you can have a look at the Entry Class as below:
                            Code:
                            class Entry 
                            {
                               public Object IdNumber;
                               public Object data;
                               public boolean value;
                            
                               public Entry(Object IdNumber, Object data, boolean value)
                             {
                                 this.IdNumber = IdNumber;
                                 this.data = data;
                                 this.value    = value;    
                              }  
                              
                                 public boolean equals(Entry e) 
                               { 
                                 return value != e.value;
                               }  
                            }
                            So I just add stuff to listOne such this:
                            Code:
                            listOne.add(new Entry( IdNumber, data, value));
                            Then, that's why I write these 3 attributes in a text file like this:
                            Code:
                            oos.writeObject ( IdNumber); //CAN I WRITE EACH OBJECT ONE BY ONE TO TEXT FILE? THEN LATER I GONNA HAVE PROBLEM HOW/WHAT IS THE CODE TO RETRIEVE THEM. 
                            oos.writeObject(data); //save each object
                            oos.writeObject(value);
                            oos.flush(); 
                            fout.close();
                            So still do I need to create an ArrayList to replace the Entry? I understand what you meant here, because later I have to retrieve them one ArrayList per ArrayList. In my case, can it be as Entry per entry?
                            Please teach me how/what is the code...thanks a lot.

                            Comment

                            • shana07
                              Contributor
                              • Jan 2007
                              • 280

                              #29
                              does anyone care to share some ideas with me about ArrayLists? Really appreciate any help. Thank You.

                              Comment

                              Working...