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

[0] = {1,2,2,3}
[1] = {2,3}
[2] = {1}
Gruszek
  • 17
  • 2

3 Answers3

2

you can defined as;

Vector<Integer> vector = new Vector();

but as you may read here: vectors are kind of old...

Vector is an obsolete Collection

and maybe you should consider using another collection like an ArrayList

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
2

Try this:

ArrayList<String> cars = new ArrayList<String>();

cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
System.out.println(cars);

I dont remember vectors in java but this is very similar. Hope it helps.

Tomaz Mazej
  • 465
  • 5
  • 19
0

You need to do some additional work, because array initialization and array element initialization are separated.

List<Integer> myIntegerVectors = new List[255];  // Array initialized
for(int i = 0;i < myIntegerVectors.length; i++)  // Elements initialized in loop
    myIntegerVectors[i] = new ArrayList<>();

myIntegerVectors[7].add(1337);
Kayaman
  • 72,141
  • 5
  • 83
  • 121