0

I want to create vector array like this in c++ in Java

vector<int> tab_u[255]

I don't have idea how to fast create. I try this:

List<Integer> tab[] = new List[255];

but i cant add something

i want something like this

tab[0] = {1,2,2,3}
tab[1] = {2,3}
tab[2] = {1}
3
  • What does it mean you cannot add something? I am missing what is your problem exactly. Commented Sep 11, 2019 at 8:51
  • tab[0] = Arrays.asList(1, 2, 2, 3);... Commented Sep 11, 2019 at 8:54
  • What happened to your previous identical question? Was it closed? Commented Sep 11, 2019 at 9:45

6 Answers 6

1
List<Integer>[] myListArray = new List<>[255];

Now you have your Array that can contain Lists of type Integer. But there is nothing in it. So to fill it you need to create the lists first, then put them in your array.

List<Integer> firstList = new ArrayList<>();  // List itself can't be instantiated, it's abstract
myListArray[0] = firstList;  // there you add the 1st element in your array to be the list you just created

Then you can fill your lists

myListArray[0].add(1);  

You can write all this "faster" with some one-liners, but honestly I wouldn't recommend it (unreadable).

FYI: it is also recommended to define your arrays like this:

int[] myIntegerArray;

instead the C-Way

int myIntegerArray[];

It's easier to read: "int - array - myIntegerArray" (the C-Way would read "int - myIntegerArray - array")

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

Comments

1

There are two ways you can approach this. You create the array and then pre-initialise an ArrayList into every index in the array:

   List<Integer>[] tab = new List[255];
   Arrays.parallelSetAll(tab, ArrayList::new);
   tab[0].add(1);
   tab[0].add(2);
   tab[0].add(2);
   tab[0].add(3);
   ....

Or, just initialise the list on the array as you allocate it

   List<Integer>[] tab = new List[255];
   tab[0] = Arrays.asList(1, 2, 2, 3);
   tab[1] = Arrays.asList(2, 3);
   tab[2] = Arrays.asList(1);

Alternatively, you can just work with a two-dimensional integer array;

        int[][] tab = new int[255][];
        tab[0] = new int[] {1, 2, 2, 3};
        tab[1] = new int[] {2, 3};
        tab[2] = new int[] {1};

Comments

0
import java.util.ArrayList;

(...)

ArrayList<Integer> l = new ArrayList<Integer>();
l.add(2);
l.add(5);

Comments

0

You can initailise array of list.

List<Integer>[] list = new ArrayList[255];

Comments

0
LinkedList<Integer> myList = new LinkedList<>()

Or

List<Integer> myList = new ArrayList<>()

For more information it is recommended to read the documantion -

https://docs.oracle.com/javase/8/docs/api/java/util/List.html

Comments

0

I think, you can also use map in here like that;

    Map<Integer, List<Integer>> vector = new HashMap<>();
    vector.put(1, Arrays.asList(1,2,2,3));
    vector.put(2, Arrays.asList(2,3));
    vector.put(3, Arrays.asList(1));

Check documentation also.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.