-
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!
-
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..
-
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...
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks