Expanding List Arrays of Lists

Why is it impossible to do something like this?

RowList<List<Value>> extends ArrayList<List<Value>> 

Also, how can this be done?

 RowList<Value> extends ArrayList<List<Value>> 

I am trying to implement a List for use in a database and want to extend the ArrayList , so I can verify that the methods .add(),.set() , etc. do not violate database requirements (without adding two lines with the same keys, etc.). I understand that this is probably not the best way to implement the database , but this is a college assignment that required us to do this.

EDIT: using the second option (which compiles), how can I access the values ​​in the lists that the RowList class has?

+4
source share
1 answer

The next two lines are identical (for the compiler), this is true even if you have a class named Value , since the named parameter will mask the class type

 class RowList<Value> extends ArrayList<List<Value>> class RowList<T> extends ArrayList<List<T>> 

The reason for this is that the value inside the first <> is a parameter of the named type. So when you try to do something like

 class RowList<List<Value>> extends ArrayList<List<Value>> 

You tried to create a named parameter of type List<Value> , which (in Java) is an invalid identifier, but instead you get the Syntax error on token(s), misplaced construct(s) error message

I think instead you are really trying to write

 public class RowList extends ArrayList<Value> { @Override public boolean add(Value e) { // TODO Custom code to check and what not return super.add(e); } } 

Where Value is a custom object in your codebase. Then elsewhere in your code, you can:

 RowList rl = new RowList(); rl.add(new Value(...)); Value v = rl.get(i); 

EDIT:

The previous example assumes that the Value class is a data entry string. If instead it is the only data item and the string is represented by List, then it will be more like the following:

 public class RowList extends ArrayList<List<Value>> { @Override public boolean add(List<Value> e) { // TODO Custom code to check and what not return super.add(e); } } RowList rl = new RowList(); List<Value> row = new ArrayList<Value>(); row.add(new Value(...)); rl.add(row); List<Value> rowGet = rl.get(i); 
+6
source

Source: https://habr.com/ru/post/1482497/


All Articles