Collection in Java Question


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Collection in Java Question

  1. #1
    Join Date
    Feb 2004
    Posts
    2

    Collection in Java Question

    Hello everyone! I am rather new to the concept of Collections in Java (I have used extensively collections in VB).

    I am trying to find how collections in java work, so I used a CASE tool that automatically generates code. I am trying to model the simplest case of a maritime company that owns some ships.

    However the code that is generated by this tool can not be compiled in JBuilder. I have two classes one called “Company” and another called “Ship”. The code is the following one (the errors that I receive are in the code):

    Code:
    //Company.java
    
    public class Company
    {
    
        
        public final Set getShips()
        {
    
            if (ships == null)
            {
                return java.util.Collections.EMPTY_SET;
            }
            return java.util.Collections.unmodifiableSet(ships);
        }
        
    
    // I GET AN ERROR IN THE FOLLOWING LINE SAYING “"Company.java": cannot resolve symbol: class Ship in class shipcompany.Company at line 31, column 32”
    
        public final void addShips(Ship arg)
        {
    
            if (arg != null)
            {
                if (ships == null)
                {
                    ships = new LinkedHashSet();
                }
                if (ships.add(arg))
                {
                    arg.setCompany(this);
                }
            }
    
        }
        
    // I GET AN ERROR FOLLOWING LINE  “Company.java": cannot resolve symbol: class Ship in class shipcompany.Company at line 48, column 35”
    
        public final void removeShips(Ship arg)
        {
    
            if (ships != null && arg != null)
            {
                if (ships.remove(arg))
                {
                    arg.setCompany(null);
                }
            }
    
        }
        
        public Company()
        {
    
        }
        
        public Set ships;
        
        protected String CName;
    
    }
    
    
    //Ship.java
    
    public class Ship
    {
    
        
        public final Company getCompany()
        {
    
            return company;
        }
        
        public final void setCompany(Company arg)
        {
    
            if (company != arg)
            {
                Company temp = company;
                company = null;//to avoid infinite recursions
                if (temp != null)
                {
                    temp.removeShips(this);
                }
                if (arg != null)
                {
                    company = arg;
                    arg.addShips(this);
                }
            }
    
        }
        
        public Ship()
        {
    
        }
        
        public Company company;
        
        protected String SName;
        
        protected int NumOfCrew;
        
        protected String TypeOfShip;
    
    }
    I have read the Java tutorial on collections, but I was wondering if there are any good code samples on Java Collections. I am after a code sample that implements a relationship 1 to many (like one Company has many ships).

    Thank you very much for your responses. I am sorry for the long post!

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    Collections is a utility class for working on objects that implement the Collection interface. After implmenting the interface, an object is guaranteed to have a number of methods that make it manipulable as a collection, and suitable for processing by the Collections utility class..

    so, per se.. there are no "Collections" in java.. there are, as you have discovered, a number of classes that represent collected data, all of which implement the Collection inteferace.

    personally, i'd have started with a simpler one:

    Code:
    public class test101{
      public static void main(String[] argv){
        java.util.ArrayList al = new java.util.ArrayList(50);
        
        //populate the arraylist
        for(int i=0; i<50; i++){
          al.add(new Double(Math.random()*1000));
        }
    
        //print out the list to show the random order
        java.util.ListIterator li = al.listIterator();
        Double d;
        while(li.hasNext()){
          d = (Double) li.next();
          System.out.println(d.toString());
        }
    
        //use the collections object to sort the list:
        Collections.sort(al);
        
        //print out the list to show the ordered list
        li = al.listIterator();
        while(li.hasNext()){
          d = (Double) li.next();
          System.out.println(d.toString());
        }
    
        //use the collections object to shuffle the list:
        Collections.shuffle(al);
        
        //print out the list to show the newly randomized list
        li = al.listIterator();
        while(li.hasNext()){
          d = (Double) li.next();
          System.out.println(d.toString());
        }
      }
    }
    that fragment demonstrates Collections... you can work through it, and use it as a base to move to a 1-M relationship based thing like a LinkedHashSet..

    i dont know which way round the LHS works, actually, if it is a linked list of hashtables or a hashtable of linked lists.. i would presume the latter.. hash collisions cause the data to e appended to the list, thus, calling get, on the header (the one company) produces a linked list of many ships..
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  3. #3
    Join Date
    Feb 2004
    Posts
    808
    also, if you could see your way to removing those long comments about errors, then the forum will return to normal..

    youre getting an arror about no Ship class either because:

    you havent compiled Ship.java

    you havent set the classpath to contain the current dirrectory ( . ) at the end...
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  4. #4
    Join Date
    Feb 2004
    Posts
    2
    Thank you very much for your reply cjard

    The error was after all caused by JBuilder.

    I compiled by source code from the command prompt and it works perfectly.

    However your comments and your piece of code are really valuable to a beginer like me!

    Thank you man!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center