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.

3
  • 4
    You're still using raw types here List<List>. Read this which addresses your last snippet of code. Commented Dec 16, 2014 at 14:48
  • 1
    this should be List<List<Object>> table = new ArrayList<List<Object>>(); and you're probably looking for List<List<?>> table = new ArrayList<List<?>>(); but this still just stores Object and not necessarily String or anything. Commented Dec 16, 2014 at 14:51
  • I actually get it now. Thank You. Commented Dec 16, 2014 at 19:40

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

1 Comment

Thank you very much for the effort. I understand it now.
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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.