1

After going through many posts and suggestions, I have found that instead of using a concrete implementation such as ArrayList, I should use List instead, to allow flexibility between different implementations of the List Interface. So far, I have seen that many programmers suggest the following line of code:

List list = new ArrayList();

However, this would give a warning in the compiler for using the raw types List and ArrayList and that they should actually be parameterized.

Synonymous to these warnings, I have found several posts telling me that raw types should never be used and that I should take advantage in using generics that java offers so conveniently.

Personally, I am trying to implement a class that acts as a table requiring a 2 Dimensional List structure with ArrayLists being used internally. I am trying to implement the following lines of code:

List<List> table;
table = new ArrayList();
table.add(new ArrayList());

Envisioned in my head, the table structure should be able to hold multiple variable types such as the raw data types along with the String variable type. I have tried to implement generics such as using

List<List<Object>> table = new ArrayList<ArrayList<Object>>();

but I have received many errors and hence failed so far.

I am relatively new to programming, pursing a computer science major, so forgive me if I have any horrible misunderstanding of the lines of code that I have exemplified above.

Thank You.

Rezwan Azfar Haleem
  • 1,198
  • 13
  • 23
  • 4
    You're still using raw types here `List`. Read [this](http://stackoverflow.com/questions/2745265/is-listdog-a-subclass-of-listanimal-why-arent-javas-generics-implicitly-p) which addresses your last snippet of code. – Sotirios Delimanolis Dec 16 '14 at 14:48
  • 1
    this should be `List> table = new ArrayList>();` and you're probably looking for `List> table = new ArrayList>();` but this still just stores `Object` and not necessarily `String` or anything. – EpicPandaForce Dec 16 '14 at 14:51
  • I actually get it now. Thank You. – Rezwan Azfar Haleem Dec 16 '14 at 19:40

3 Answers3

2

You want to do this:

List<List<Foo>> table = new ArrayList<List<Foo>>();
table.add(new ArrayList<Foo>())

Where Foo is the type stored in your table.

You want the generic parameter of the type and value to be the same.

  • It looks so simple and yet I couldn't figure it out. I feel relieved that I will not get more of those warnings and still use generics. Thank you very much. – Rezwan Azfar Haleem Dec 16 '14 at 19:35
1
import java.util.ArrayList;
import java.util.List;

public class Sample<T> {

    private final int x;
    private final int y;
    private final List<List<T>> list;

    public Sample(final int x, final int y) {
        this.x = x;
        this.y = y;
        list = new ArrayList<>();
        for(int k=0; k<y; k++) {
           list.add(k, new ArrayList<T>());
        }
    }


    public T get(final int indexX, final int indexY) {
        if(indexX >= x) {
            return null;
        }
        if(indexY >= y) {
            return null;
        }
        return list.get(indexX).get(indexY);
    }

Now you can call Sample<String> s = new Sample<>(); and done. Hope it answer your query.

Trying
  • 14,004
  • 9
  • 70
  • 110
0

You should make your class generic to avoid warnings/errors. Maybe this small class that I wrote will help you out:

import java.util.ArrayList;
import java.util.List;

/**
* Creates a List of Lists of the given type
* @param <T> - The type of the table elements
*/
public class Table <T> {
    private final List<List<T>> data;

    public Table(int rows, int cells) {
        data = new ArrayList<List<T>>(rows);
        for(int i=0; i<rows; i++) {
            data.add(new ArrayList<T>(cells));
        }
    }

    public static void main(String[] args) {
        //create a table of strings
        Table<String> table = new Table<String>(10, 10);
        //do something with table
    }
}

If you want the table to contain various elements, create it like that:

Table<Object> table = new Table<Object>(10, 10);

This should only serve to demonstrate generics, I'm not claiming this is the best way to create a table. Also, I'm skipping the implementation of other methods you will surely need (such as accessors for the table elements, etc).

John Smith
  • 1,559
  • 1
  • 12
  • 18