7 Replies Latest reply on Apr 20, 2008 2:37 AM by 807601

    searching and organising arraylist

    807601
      Hello again :)

      i have the following:-
      //scanner keyboard
           Scanner kybd = new Scanner(System.in);
                
      // create company
           BoatHireCompany octagon = new BoatHireCompany("octagon", 10);
                
      // add default boats to octagon company
           octagon.addBoat(new RowingBoat("Titanic", 11));
           octagon.addBoat(new PedalBoat("Red", 12));
           octagon.addBoat(new PedalBoat("Green", 13));
      and some user input of boat:-
      System.out.println("Enter the boat colour:");
      name = kybd.next();
      System.out.println("Enter boat ID:");
      int id = kybd.nextInt();
      octagon.addBoat(new RowingBoat(name, id));
      can someone please tell me how i can organise the arraylist by the id (int)?

      also could someone please tell me how i could print all the pedalboat ones?

      many thanks in advance.

      dave.
        • 1. Re: searching and organising arraylist
          807601
          can someone please tell me how i can organise the arraylist by the id (int)?
          I don't see any ArrayList in your code.

          Anyway, you can sort Lists with java.util.Collections.sort.
          also could someone please tell me how i could print all the pedalboat ones?
          You can iterate through the list (use a for loop), and pick out the ones you want.
          • 2. Re: searching and organising arraylist
            807601
            Hello, thanks for the reply. sorry the array list was in a different file.

            I got the ordering working but confused about the iterate with a for loop.

            i have a boat class which has sub classes of pedalboat and rowingboat. the arraylist is then in the boathirecompany. you can see an older version (although shows you most things) of the old code - http://forum.java.sun.com/thread.jspa?threadID=5287605&tstart=20

            is it possible if you give me an example to try and find all the objects which use either the pedalboat or rowingboat? it would be much appreciated :)

            dave.
            • 3. Re: searching and organising arraylist
              807601
              You probably just should have continued that thread rather than creating another one.

              Anyway, you can check to see if an object is from a particular kind of class using the instanceof operator, but usually that's a bad thing to do.

              Why are you trying to find objects of a particular class? What are you going to do with them?
              • 4. Re: searching and organising arraylist
                807601
                hello, sorry if i created another thread. it was intential.

                i think i have sorted it by doing a boolean and then checking if an object has the boolean set to true which seems to have done the trick :). don't know if it is best practice but i am new and it is working so i am happy.

                thanks for the help and suggestions.

                dave.
                • 5. Re: searching and organising arraylist
                  807601
                  monkey124 wrote:
                  hello, sorry if i created another thread. it was intential.

                  i think i have sorted it by doing a boolean and then checking if an object has the boolean set to true which seems to have done the trick :). don't know if it is best practice but i am new and it is working so i am happy.
                  No, instanceof would be best practice in this case. Adding a boolean flag is not very extensible is it? What if next time you need to print out all the rowboats? Have a flag for rowboat and a flag for a paddleboat? What if after that you need to get all the steamships?

                  It's really simple, you should've followed the advice Paul gave you.

                  Try this code:
                  Boat boat1 = new PedalBoat("blah", 1);
                  Boat boat2 = new RowBoat("blah", 2);
                  if ( boat1 instanceof PedalBoat ) {
                     System.out.println(boat1);
                  }
                  if ( boat2 instanceof PedalBoat ) {
                     System.out.println(boat2);
                  }
                  • 6. Re: searching and organising arraylist
                    807601
                    that was simple....what was i thinking.

                    thanks everyone for all your help :)
                    • 7. Re: searching and organising arraylist
                      807601
                      The reason I asked what you were trying to accomplish, is that when you want to check to see whether an object is of a certain type, it often means that you need to restructure your code such that the check isn't necessary.

                      Still, instanceof is better than adding a boolean field whose only purpose is to duplicate the instanceof.