Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

Beginners Question: Typesafe collection with 1.5

Thread view: 
scherer_mike@web.de - 18 Jun 2006 10:51 GMT
dear all,
i have a little problem of understanding and i am not able to get out
of it:
et voilà the snippets:

with java 1.4 i wrote:
// -------------------------------------------------------
myclass cc = ...;
...
List myList;
myList = new ArrayList();
...
myList.add(cc);

same with 1.5 to make it typesafe:
// -------------------------------------------------------
myclass cc = ...;
...
List < myclass > myList;
myList = new < myclass > ArrayList();
...
myList.add(cc);

so far, so good ... but now the snippet with 2 dimensional array of
ArrayList with 1.4
// -------------------------------------------------------
myclass cc = ...;
...
List myTable[][] ;
mytable = new ArrayList[n][n];
...
myTable[a][b].add(cc);

this works well with 1.4. but this last snippet typesafe with 1.5 ... i
cannot solve! Whereever i place the < type > thingy in the source, i
get an error message.

could you advise me, please?

thank you very much,
Michael
Thomas Hawtin - 18 Jun 2006 11:36 GMT
> myclass cc = ...;
> ....
[quoted text clipped - 6 lines]
> cannot solve! Whereever i place the < type > thingy in the source, i
> get an error message.

List<List<List<MyClass>>> myTable =
    new ArrayList<List<List<MyClass>>>();

Prefer collections to arrays of references.

You are probably better off introducing some sort of table class, rather
than working with little abstraction.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

scherer_mike@web.de - 18 Jun 2006 16:05 GMT
Thomas Hawtin schrieb:

> > myclass cc = ...;
> > ....
[quoted text clipped - 9 lines]
> List<List<List<MyClass>>> myTable =
>      new ArrayList<List<List<MyClass>>>();

hi tom,

thank you very much. but it does not work this way.
in my example i would have better written
mytable = new ArrayList[N][M] for a fix N, M.
with your hint i get the compiler-message: "The type of the expression
must be an array type but it resolved to List<List<List<mmCode>>>."

but in general i agree: the data abstraction is poor. i am also a
novice in this kind of object-modelling.

thank you very much
Oliver Wong - 19 Jun 2006 22:17 GMT
> Thomas Hawtin schrieb:
>
[quoted text clipped - 22 lines]
> but in general i agree: the data abstraction is poor. i am also a
> novice in this kind of object-modelling.

   Have you considered creating a new class to represent your tables?

class Table<T> {
 final private FixedLengthList<FixedLengthList<T>> data;
 final private int width, height;

 public Table(int width, int height) {
   this.data = new FixedLengthList<List<T>>(width);
   this.width = width;
   this.height = height;
 }

 public T get(int x, int y) {
   this.data.get(x).get(y);
 }
 /*put more code here*/
}

class FixedLengthList<T> extends AbstractList<T> {
 private final int length;

 public FixedLengthList(int length) {
   this.length = length;
 }

 /*put more code here*/
}

   - Oliver


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.