Beginners Question: Typesafe collection with 1.5

S

scherer_mike

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].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
 
T

Thomas Hawtin

myclass cc = ...;
....
List myTable[][] ;
mytable = new ArrayList[n][n];
....
myTable[a].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.


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
 
S

scherer_mike

Thomas said:
myclass cc = ...;
....
List myTable[][] ;
mytable = new ArrayList[n][n];
....
myTable[a].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.


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
 
O

Oliver Wong

Thomas said:
myclass cc = ...;
....
List myTable[][] ;
mytable = new ArrayList[n][n];
....
myTable[a].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.


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.


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top