compare two ArrayLists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shana07
    Contributor
    • Jan 2007
    • 280

    #1

    compare two ArrayLists

    Friends, I need some inputs about my java program. My program is supposed to store 3 information into an ArrayList so-called arrayOne at each runtime:
    1. Unique ID
    2. Boolean Value
    3. data.

    Let's assume I give first input a = 5, compile & run: Result - arrayOne.
    Second Input a = 24, compile & run: Result - arrayOne.
    Aim: To compare these two result arrays after runtime.

    How to extract the results & compare these 2 result arrays after runtime that can automatically check the data & boolean value?

    please advise and I'll post my program then...
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by shana07
    Friends, I need some inputs about my java program. My program is supposed to store 3 information into an ArrayList so-called arrayOne at each runtime:
    1. Unique ID
    2. Boolean Value
    3. data.

    Let's assume I give first input a = 5, compile & run: Result - arrayOne.
    Second Input a = 24, compile & run: Result - arrayOne.
    Aim: To compare these two result arrays after runtime.

    How to extract the results & compare these 2 result arrays after runtime that can automatically check the data & boolean value?

    please advise and I'll post my program then...
    If you want the results after the program has exited then you need to write the results to a file or database.

    Comment

    • shana07
      Contributor
      • Jan 2007
      • 280

      #3
      Originally posted by r035198x
      If you want the results after the program has exited then you need to write the results to a file or database.
      Please give me some pointer on how to write result into a file ..
      and compare them ...at least any web page for my reading..thanks a lot

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by shana07
        Please give me some pointer on how to write result into a file ..
        and compare them ...at least any web page for my reading..thanks a lot
        Last week's tip of the week talked about writing to a textfile. In this case however you should write the Arraylist to the file as an object using serialization.

        Comment

        • shana07
          Contributor
          • Jan 2007
          • 280

          #5
          Originally posted by r035198x
          Last week's tip of the week talked about writing to a textfile. In this case however you should write the Arraylist to the file as an object using serialization.
          Yes, I've gone through it and I do remember last time you taught me how to transfer/export result into a file using FileOutputStrea m.
          Now I managed to get it done. Next, how am I supposed to do to compare the second result(new input) after second program has exited?
          hope you can guide me please...

          o ya I print result in a txt file...or maybe I need to print in other format in order for me to compare them automatically? idea please...thank you

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by shana07
            Yes, I've gone through it and I do remember last time you taught me how to transfer/export result into a file using FileOutputStrea m.
            Now I managed to get it done. Next, how am I supposed to do to compare the second result(new input) after second program has exited?
            hope you can guide me please...

            o ya I print result in a txt file...or maybe I need to print in other format in order for me to compare them automatically? idea please...thank you
            If you wrote the ArrayList to the file as an object like you should have then you can retrieve it using the readObject method of ObjectInputStre am and type cast it to the ArrayList.

            Comment

            • shana07
              Contributor
              • Jan 2007
              • 280

              #7
              Originally posted by r035198x
              If you wrote the ArrayList to the file as an object like you should have then you can retrieve it using the readObject method of ObjectInputStre am and type cast it to the ArrayList.
              From my understanding, what you mean is while the second program is still running retrieve the first output from text file using the readObject() from ObjectInputStre am class and automatically compare them?

              please correct me if I'm wrong...

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by shana07
                From my understanding, what you mean is while the second program is still running retrieve the first output from text file using the readObject() from ObjectInputStre am class and automatically compare them?

                please correct me if I'm wrong...
                I'm not too sure about the "automatica lly" part but that's the jists of it.

                Comment

                • shana07
                  Contributor
                  • Jan 2007
                  • 280

                  #9
                  Originally posted by r035198x
                  I'm not too sure about the "automatica lly" part but that's the jists of it.
                  Alright, thanks for your valuable idea. Please guide me how to retrieve the first executed program result...Do I need to call readObject() from the ArrayList place or create another new method - ObjectInputStre am or from here:
                  Code:
                  public static void print(String s) 
                    {      
                        try
                          {
                              FileOutputStream fout;	
                  	    fout = new FileOutputStream ("output.txt",true);    
                              new PrintStream(fout).println (s);   
                  	    fout.close();
                          }
                  
                        catch (IOException ex) 
                        {
                  	      System.out.println("There was a problem creating/writing to the temp file");
                  	      ex.printStackTrace();
                        }   
                    }

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by shana07
                    Alright, thanks for your valuable idea. Please guide me how to retrieve the first executed program result...Do I need to call readObject() from the ArrayList place or create another new method - ObjectInputStre am or from here:
                    Code:
                    public static void print(String s) 
                    { 
                    try
                    {
                    FileOutputStream fout;	
                    	 fout = new FileOutputStream ("output.txt",true); 
                    new PrintStream(fout).println (s); 
                    	 fout.close();
                    }
                     
                    catch (IOException ex) 
                    {
                    	 System.out.println("There was a problem creating/writing to the temp file");
                    	 ex.printStackTrace();
                    } 
                    }
                    What is that code suppposed to be doing?

                    You should write the ArrayList to the file using an ObjectOutputstr eam first in the first program, then you can retrieve it from the file in the second program

                    Comment

                    • shana07
                      Contributor
                      • Jan 2007
                      • 280

                      #11
                      Originally posted by r035198x
                      What is that code suppposed to be doing?

                      You should write the ArrayList to the file using an ObjectOutputstr eam first in the first program, then you can retrieve it from the file in the second program
                      In general the final result of my program is to collect/store 3 types of data:
                      1. Unique ID
                      2. Boolean Value
                      3. data.
                      I use ArrayList to store those information
                      Code:
                      List listOne = new ArrayList();
                      ....
                      //looping for collect required data
                      ....
                      listOne.add(new Entry(ID, data, value));  
                                          print("ID: " +uniqueID);
                                          print("Data: " +data);
                                          print("value: " +value);
                      Now you mean I have to change the ArrayList to the file using an ObjectOutputstr eam? But how..please gimme some pointer...

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by shana07
                        In general the final result of my program is to collect/store 3 types of data:
                        1. Unique ID
                        2. Boolean Value
                        3. data.
                        I use ArrayList to store those information
                        Code:
                        List listOne = new ArrayList();
                        ....
                        //looping for collect required data
                        ....
                        listOne.add(new Entry(ID, data, value)); 
                        print("ID: " +uniqueID);
                        print("Data: " +data);
                        print("value: " +value);
                        Now you mean I have to change the ArrayList to the file using an ObjectOutputstr eam? But how..please gimme some pointer...
                        You write the whole arraylist as one object to a file and then retrieve it later. You write it to a file using ObjectOutputStr eam. Look at the docs for this class and see how can use it to store the whole arraylist into a file.

                        Comment

                        • shana07
                          Contributor
                          • Jan 2007
                          • 280

                          #13
                          Originally posted by r035198x
                          You write the whole arraylist as one object to a file and then retrieve it later. You write it to a file using ObjectOutputStr eam. Look at the docs for this class and see how can use it to store the whole arraylist into a file.
                          Firstly, I've tried to write ArrayList as one object to a file, is that something like this? please check as I compile everything okay..but problem during runtime.
                          Code:
                          try
                                  {
                                      //  Create a stream for writing.
                                      FileOutputStream fout  = new FileOutputStream ("output.txt",true);  
                                      //  Next, create an object that can write to that file.
                                      ObjectOutputStream oOutStream = new ObjectOutputStream( fout );
                                      
                                      //  Save each object.
                                      oOutStream.writeObject(listOne);            
                                      
                                      oOutStream.flush();            
                                      new PrintStream(fout).println (s);   
                                      fout.close();         
                                  
                                  }

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by shana07
                            Firstly, I've tried to write ArrayList as one object to a file, is that something like this? please check as I compile everything okay..but problem during runtime.
                            Code:
                            try
                            {
                            // Create a stream for writing.
                            FileOutputStream fout = new FileOutputStream ("output.txt",true); 
                            // Next, create an object that can write to that file.
                            ObjectOutputStream oOutStream = new ObjectOutputStream( fout );
                             
                            // Save each object.
                            oOutStream.writeObject(listOne); 
                             
                            oOutStream.flush(); 
                            new PrintStream(fout).println (s); 
                            fout.close(); 
                             
                            }
                            Should be fine. I'm not sure what the line

                            Code:
                            new PrintStream(fout).println (s);
                            is doing there though

                            Comment

                            • shana07
                              Contributor
                              • Jan 2007
                              • 280

                              #15
                              Originally posted by r035198x
                              Should be fine. I'm not sure what the line

                              Code:
                              new PrintStream(fout).println (s);
                              is doing there though
                              Still the same if I comment the line ..it's okay. i'll figure it out...
                              How about to retrieve back the data...? Let say after I finish this phase which is in the first program run - write result into a file namely A.
                              Second time I run program with different input produce new result, I do comparison directly in here by retrieve file A? Do I have to write to a new file namely B first or how, any idea...? thank you

                              Comment

                              Working...