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?
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?