4 Replies Latest reply: Jul 13, 2008 8:32 PM by 807589 RSS

    contains method used with arraylist

    807589
      hi
      i have a arraylist that holds coordinates x,y etc

      this is part of the code that im currently using but to me it seems slow in the sence
      that i could make it better.
      it goes through the whole list one by one in search of a possible match then shows the index number
      then removes the item at the index number.

      my question is, is there a easier way maybe using the contains method that if found a possible match
      would also show the index number of that match so then i can remove it
          // Declear ints 
          int x;
          int y;
          int xDrag;
          int yDrag;
          int shapeInt;
          String splitStr[];
          
          if (!lineslist.isEmpty()){
          
          for (int i = 0; i < lineslist.toArray().length ; i++){
              
          splitStr = lineslist.get(i).toString().split(" "); 
          x =  Integer.parseInt(splitStr[0]);
          y =  Integer.parseInt(splitStr[1]);
          xDrag = Integer.parseInt(splitStr[2]);
          yDrag = Integer.parseInt(splitStr[3]);
          shapeInt = Integer.parseInt(splitStr[4]);    
              
          if(Intersect.intersects(x, y, xDrag, yDrag)){
          System.out.println("intersect found");  
          
          // remove item at
          lineslist.remove(i);
          }
          
          }
          }
         
      if someone could show me a example that would be great:)
        • 1. Re: contains method used with arraylist
          807589
          You appear to have an array list of string: ArrayList<String>. Why not have an ArrayList of objects of a class that you've created: ArrayList<MyClass> that contains your x, y, xDrag.... data as ints?
          • 2. Re: contains method used with arraylist
            807589
            yea but it wouldn't be on the same line e.g.

            x +" " + y + " " + xDrag + " " + yDrag

            if i did an array list of int's it would be like this
            x
            y
            xdrag
            ydrag

            it wouldnt be a whole it would be seperate if you get what i mean

            Edited by: nicchick on Jul 13, 2008 6:23 PM
            • 3. Re: contains method used with arraylist
              807589
              No, you don't understand. I'm talking of using OOPs like it should be used. to wit:
              a class that holds some data:
              public class MyPoint
              {
                private int x, y;
                private int width, height;
                public MyPoint(int x, int y, int width, int height)
                {
                  this.x = x;
                  this.y = y;
                  this.width = width;
                  this.height = height;
                }
                public int getX()
                {
                  return x;
                }
                public int getY()
                {
                  return y;
                }
                public int getWidth()
                {
                  return width;
                }
                public int getHeight()
                {
                  return height;
                }
                
                @Override
                public String toString()
                {
                  return "(" + x + ", " + y + ", " + width + ", " + height + ")";
                }
              }
              a class that has a list that holds objects of the class above:
              import java.util.ArrayList;
              import java.util.List;
              
              public class UsesMyPoint
              {
                private List<MyPoint> myPointList = new ArrayList<MyPoint>();
                
                public UsesMyPoint()
                {
                  for (int i = 0; i < 20; i++)
                  {
                    myPointList.add(new MyPoint(i, i + 10, 20, 20));
                  }
                  
                  for (int i = 0; i < myPointList.size(); i++)
                  {
                    System.out.println("point " + i + " has y set at: " + myPointList.get(i).getY());
                  }
                }
                
                public static void main(String[] args)
                {
                  new UsesMyPoint();
                }
              }
              • 4. Re: contains method used with arraylist
                807589
                nicchick wrote:
                yea but it wouldn't be on the same line e.g.

                x +" " + y + " " + xDrag + " " + yDrag

                if i did an array list of int's
                That was not what was suggested.
                ... if you get what i mean
                It would appear you did not get it.
                The suggestion was that you create a class to hold 5 ints; x, y, xDrag, yDrag, shape
                and store objects of said class in your ArrayList.

                If you want you can add a toString() method that produces your familiar
                x +" " + y + " " + xDrag + " " + yDrag