Trying to implement generics

I'm been trying to work on my code that I developed for a school assignment to make it better. One of the problems I ran in to with that assignment was the -Xlint:unchecked thing so I'm trying to fix that using generics. So I put <Object> in some places to make the warning stop showing up, but when I replace <Object> with the actual type I intend to use like <Integer> or <String> it throws an error at me. Specifically this:

TableContainer.java:26: cannot find symbol

symbol : method add(java.util.ArrayList)

location: class java.util.ArrayList<java.lang.Integer>

tableData.add(newTable);

1 error

Here is my code:

publicclass TableContainer{

// Overview: TableContainer class that provides methods for

// making a table containing other tables.

private ArrayList<Object> tableData;

public TableContainer()

{

tableData =new ArrayList<Object>();

}

public TableContainer(ArrayList<Object> objData)

{

tableData = objData;

}

publicvoid addTable(int elemNum)

{

ArrayList newTable =new ArrayList(elemNum);

tableData.add(newTable);

}

}

Anytime I replace <Object> with <Integer> or anything else, it doesn't work. Any ideas of why?

[1950 byte] By [Lord_Butlera] at [2007-10-3 6:02:01]
# 1

private ArrayList<Integer> tableData;

When you do this the only objects allowed in tableData are Integers.

ArrayList newTable = new ArrayList(elemNum);

tableData.add(newTable);

Is newTable an Integer?

floundera at 2007-7-15 0:44:09 > top of Java-index,Java Essentials,New To Java...
# 2

I noticed I had a mistake in my code. Here is a fix, elemNum is supposed to be of type ArrayList.

public class TableContainer

{

// Overview: TableContainer class that provides methods for

// making a table containing other tables.

private ArrayList<Object> tableData;

public TableContainer()

{

tableData = new ArrayList<Object>();

}

public TableContainer(ArrayList<Object> objData)

{

tableData = objData;

}

public void addTable(ArrayList<Object> elem)

{

ArrayList newTable = new ArrayList<Object>(elem);

tableData.add(newTable);

}

public void printTable()

{

for(int rownum = 0; rownum < tableData.size(); rownum++)

{

System.out.println(tableData.get(rownum));

}

}

}

I did try the private ArrayList<Integer> tableData; that you suggested, but it didn't seem to work. Here is my new code:

public class TableContainer

{

// Overview: TableContainer class that provides methods for

// making a table containing other tables.

private ArrayList<Integer> tableData;

public TableContainer()

{

tableData = new ArrayList<Integer>();

}

public TableContainer(ArrayList<Integer> objData)

{

tableData = objData;

}

public void addTable(ArrayList<Integer> elem)

{

ArrayList<Integer> newTable = new ArrayList<Integer>(elem);

tableData.add(newTable);

}

public void printTable()

{

for(int rownum = 0; rownum < tableData.size(); rownum++)

{

System.out.println(tableData.get(rownum));

}

}

}

And I get this error: TableContainer.java:28: cannot find symbol

symbol : method add(java.util.ArrayList<java.lang.Integer>)

location: class java.util.ArrayList<java.lang.Integer>

tableData.add(newTable);

1 error

Any more ideas why it is doing this?

Lord_Butlera at 2007-7-15 0:44:09 > top of Java-index,Java Essentials,New To Java...
# 3
Did you read reply #1?
floundera at 2007-7-15 0:44:09 > top of Java-index,Java Essentials,New To Java...
# 4
newTable is of Integer type, sorry :D
Lord_Butlera at 2007-7-15 0:44:09 > top of Java-index,Java Essentials,New To Java...
# 5
No it is not. It is an ArrayList!!!
floundera at 2007-7-15 0:44:09 > top of Java-index,Java Essentials,New To Java...
# 6
Yeah, its an ArrayList.Although I can kind of see why you think newTable is of type Integer.
toruma at 2007-7-15 0:44:09 > top of Java-index,Java Essentials,New To Java...