java.util.List

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

    java.util.List

    Hi again, would like to consult about List problem.

    //start code
    Code:
     java.util.List ifs = new java.util.ArrayList();
    // we're in the 'getMonitor' method
    Code:
       Instruction insn = jvm.getLastInstruction();     
       ……… 
       ………
       if (insn instanceof IfInstruction) 
    {
         IfInstruction iff = (IfInstruction) insn;
         Object position = insn.getOffset();
         boolean value = iff.getConditionValue();
         ifs.add(new Entry(position, value));
    
        System.out.print(position);
        System.out.println();
        System.out.print(value);
        System.out.println();
                                    
        Iterator iter = ifs.iterator();
        while (iter.hasNext()) {
                      System.out.println("string " + iter.next());
                      }
        System.out.print(ifs.size());
        System.out.println();              
       }
    // ... the rest of the method are here
    Code:
    class Entry {
       public Object position;
       public boolean value;
    
       public Entry(Object position, boolean value) {
         this.position = position;
         this.value    = value;
       }
    }
    Now what happen is I need to call those fields above in a method ‘report’
    (in the same file – TestDriver.java ). Here comes my question:
    1. How am I supposed to call single value/position without call Entry as a whole?
    E.g: I want to compare value of index 1 and index 0 - which I have read inputs from Array.
    Code:
    void report( )
    ………
    ………
    //ifs.get(1) != ifs.get(0) 
    //I don't need this because it is getting compare the
    //whole Entry.I need something like this: 
    
    if (ifs.value(1) != ifs.value(0)) //For sure errors encountered! 
       {
            System.out.println (“A”);
        }
        else 
        {
            System.out.println (“B”);
        }
    Please advise me. thanks guys...
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by shana07
    Hi again, would like to consult about List problem.

    //start code
    Code:
     java.util.List ifs = new java.util.ArrayList();
    // we're in the 'getMonitor' method
    Code:
    Instruction insn = jvm.getLastInstruction(); 
    ……… 
    ………
    if (insn instanceof IfInstruction) 
    {
    IfInstruction iff = (IfInstruction) insn;
    Object position = insn.getOffset();
    boolean value = iff.getConditionValue();
    ifs.add(new Entry(position, value));
     
    System.out.print(position);
    System.out.println();
    System.out.print(value);
    System.out.println();
     
    Iterator iter = ifs.iterator();
    while (iter.hasNext()) {
    System.out.println("string " + iter.next());
    }
    System.out.print(ifs.size());
    System.out.println(); 
    }
    // ... the rest of the method are here
    Code:
    class Entry {
    public Object position;
    public boolean value;
     
    public Entry(Object position, boolean value) {
    this.position = position;
    this.value = value;
    }
    }
    Now what happen is I need to call those fields above in a method ‘report’
    (in the same file – TestDriver.java ). Here comes my question:
    1. How am I supposed to call single value/position without call Entry as a whole?
    E.g: I want to compare value of index 1 and index 0 - which I have read inputs from Array.
    Code:
    void report( )
    ………
    ………
    //ifs.get(1) != ifs.get(0) 
    //I don't need this because it is getting compare the
    //whole Entry.I need something like this: 
     
    if (ifs.value(1) != ifs.value(0)) //For sure errors encountered! 
    {
    System.out.println (“A”);
    }
    else 
    {
    System.out.println (“B”);
    }
    Please advise me. thanks guys...
    Do you have objects of type Entry in the ArrayList? Do you want to compare two Entry objects using the attribute position only?

    Comment

    • shana07
      Contributor
      • Jan 2007
      • 280

      #3
      I’m not too sure to answer your Q. What I’ve understood is Yes I have objects of type Entry. First I ran those codes with int a[0] and try to record the attribute value at each IF* bytecode. So the output from this code giving – 4 Entry for first execution.
      Code:
      Iterator iter = ifs.iterator();
      while (iter.hasNext()) 
      {
         System.out.println("string " + iter.next());  }
      System.out.print(ifs.size());
      Then I ran again the execution to execute second int a value from array and try to compare the current ifs.value of int a[1] with previous ifs.value of int a[0]. So in short - to compare two Entry of two objects int a[1] and int a[0] using attribute ‘value’ only.

      Comment

      • shana07
        Contributor
        • Jan 2007
        • 280

        #4
        Originally posted by shana07
        Then I ran again the execution to execute second int a value from array and try to compare the current ifs.value of int a[1] with previous ifs.value of int a[0]. So in short - to compare two Entry of two objects int a[1] and int a[0] using attribute ‘value’ only.
        Sorry for confusing you guys..I think to compare two collection objects (lists) for the attribute 'value' only. e.g
        first List - run execution with int a = 1 until finish
        second List - run execution with in a = 5 until finish and this time I need to compare the attribute 'value' for the two Lists. ...

        let me know if still dont get it....
        Shana

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by shana07
          I’m not too sure to answer your Q. What I’ve understood is Yes I have objects of type Entry. First I ran those codes with int a[0] and try to record the attribute value at each IF* bytecode. So the output from this code giving – 4 Entry for first execution.

          Code:
          Iterator iter = ifs.iterator();
          
          while (iter.hasNext()) 
          
          {
          
          System.out.println(\"string \" + iter.next()); }
          
          System.out.print(ifs.size());
          Then I ran again the execution to execute second int a value from array and try to compare the current ifs.value of int a[1] with previous ifs.value of int a[0]. So in short - to compare two Entry of two objects int a[1] and int a[0] using attribute ‘value’ only.
          To compare two Entry objects using value only, add a compareTo method in the Entry class which compares the values

          Code:
           public int compareTo (Entry e) { 
          
          if(value < e.value) {
          
             return -1;
          
          }
          
          else if(value == 0) {
          
             return 0;
          
          }
          
          else {
          
          return 1;
          
          }
          
          }
          Now simply use that method to compare two Entry objects using value only.

          Comment

          • shana07
            Contributor
            • Jan 2007
            • 280

            #6
            Could you please tell me why I got this error?
            java.lang.Error : Unresolved compilation problem:
            The method compareTo(Objec t) is undefined for the type TestDriver
            I call method compareTo such this:
            (as mentioned above I want to compare two Entry objects)
            Code:
             void report (String header) 
              {
                final int EQUAL = 0;
                System.out.println(header);
                for (int i = 1; i < ifs.size(); i++) 
                {
                   int comparison = compareTo(ifs.get(0));
                   if (comparison != EQUAL) 
                   {
               System.out.println ();
              System.out.println ("A DIFFERENT BRANCH HAS BEEN FOUND:" +IfCount);
              break;
                        }
                    else 
                    {
                System.out.println ("FAILED TO FIND A DIFFERENT BRANCH:" +IfCount);
                break;
                    }       
                }

            Comment

            • shana07
              Contributor
              • Jan 2007
              • 280

              #7
              Maybe you can help me to explain in short what's actually doing in method 'compareTo' especially when you give return 1 and etc.

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by shana07
                Maybe you can help me to explain in short what's actually doing in method 'compareTo' especially when you give return 1 and etc.
                The compareTo method is a method that you are using to compare 2 Entry objects. This can result in three possiblities. the objects might be equal (return 0) the current Object might be less than the given object (return -1) while the other option returns 1. the return values are what we will check to see the result of the comparison.

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by shana07
                  Could you please tell me why I got this error?


                  I call method compareTo such this:
                  (as mentioned above I want to compare two Entry objects)
                  Code:
                  void report (String header) 
                  {
                  final int EQUAL = 0;
                  System.out.println(header);
                  for (int i = 1; i < ifs.size(); i++) 
                  {
                  int comparison = compareTo(ifs.get(0));
                  if (comparison != EQUAL) 
                  {
                  System.out.println ();
                  System.out.println ("A DIFFERENT BRANCH HAS BEEN FOUND:" +IfCount);
                  break;
                  }
                  else 
                  {
                  System.out.println ("FAILED TO FIND A DIFFERENT BRANCH:" +IfCount);
                  break;
                  } 
                  }
                  You said you wanted to compare two Entry objects, so you should put the method compareTo inside the Entry class.

                  Comment

                  • shana07
                    Contributor
                    • Jan 2007
                    • 280

                    #10
                    Alright in my case, what need to be compared is 'boolean' value. I want to compare (currentobject != previousobject) - in terms of values either both are TRUE/FALSE.
                    Code:
                    public int compareTo (Entry e) 
                       { 
                          if(value != e.value) 
                           {
                               return -1;
                            }
                           else if(value == e.value)
                           {
                               return 0;
                           }
                           else 
                           {
                               return 1;
                           }
                        }
                    could you please take a look at the error above....

                    Comment

                    • shana07
                      Contributor
                      • Jan 2007
                      • 280

                      #11
                      Originally posted by r035198x
                      You said you wanted to compare two Entry objects, so you should put the method compareTo inside the Entry class.
                      Yes I did put it....
                      Code:
                      class Entry
                      {
                         public Object position;
                         public boolean value;
                      
                         public Entry(Object position, boolean value)
                       {
                           this.position = position;
                           this.value    = value;
                         }
                         
                         public int compareTo (Entry e) 
                         { 
                            if(value != e.value) 
                             {
                                 return -1;
                              }
                             else if(value == e.value)
                             {
                                 return 0;
                             }
                             else 
                             {
                                 return 1;
                             }
                          }  
                      }

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by shana07
                        Yes I did put it....
                        Code:
                        class Entry
                        {
                        public Object position;
                        public boolean value;
                         
                        public Entry(Object position, boolean value)
                        {
                        this.position = position;
                        this.value = value;
                        }
                         
                        public int compareTo (Entry e) 
                        { 
                        if(value != e.value) 
                        {
                        return -1;
                        }
                        else if(value == e.value)
                        {
                        return 0;
                        }
                        else 
                        {
                        return 1;
                        }
                        } 
                        }
                        If you are using a boolean value for comparison then simply override the equals method
                        as
                        Code:
                         public boolean equals(Entry e) { 
                          return value == e.value;
                        }
                        The method is in the Entry class, so you call it using an instance of the Entry class.
                        Something like
                        Code:
                         Entry e = ....... 
                        if(e.equals(Entry anotherEntry)) {
                          //do stuff
                        }
                        else {
                        //do something else
                        }
                        calling it in some other class as just equals(e) (like you did with the compareTo) does not make sense and would give incorect results for the equals method and a compilation error for the compareTo method.

                        Comment

                        • shana07
                          Contributor
                          • Jan 2007
                          • 280

                          #13
                          Thank you bro...short and clear explanation.
                          After override with 'equals' and made some adjustment Entry e to Object o (because prompted error something uncompatible Entry - Object...) I got what I want now.
                          Code:
                           Object o = ifs.get(4);
                                  if(o.equals(ifs.get(0)))
                          Thanks a bunch.......... .
                          Shana

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by shana07
                            Thank you bro...short and clear explanation.
                            After override with 'equals' and made some adjustment Entry e to Object o (because prompted error something uncompatible Entry - Object...) I got what I want now.
                            Code:
                             Object o = ifs.get(4);
                            if(o.equals(ifs.get(0)))
                            Thanks a bunch.......... .
                            Shana
                            Anytime. It took me a while to understand what you wanted to do there though.

                            Comment

                            • Ganon11
                              Recognized Expert Specialist
                              • Oct 2006
                              • 3651

                              #15
                              Originally posted by shana07
                              Thank you bro...short and clear explanation.
                              After override with 'equals' and made some adjustment Entry e to Object o (because prompted error something uncompatible Entry - Object...) I got what I want now.
                              Code:
                               Object o = ifs.get(4);
                                      if(o.equals(ifs.get(0)))
                              Thanks a bunch.......... .
                              Shana
                              You may end up having some loss of information when casting to an Object to compare. Instead, you may want to case ifs.get() to Entry, as in:

                              Code:
                              Entry myEnt = (Entry)ifs.get(4);
                              if (myEnt.equals((Entry)ifs.get(0))) {
                                 // And so on...

                              Comment

                              Working...