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}
4
  • Vector<Integer> vector = new Vector<Integer>(255) Commented Sep 11, 2019 at 7:23
  • ArrayList<String> cars = new ArrayList<String>(255); Commented Sep 11, 2019 at 7:34
  • A quick look at oracle guide could save you from embarrassing situations like this one. Commented Sep 11, 2019 at 8:46
  • ArrayList<ArrayList<String>> cars = new ArrayList<>(255); Commented Sep 11, 2019 at 10:41

3 Answers 3

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

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

Comments

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.

2 Comments

To make clear you are using an interface and a special implementation of it, you could have written List<String> cars = new ArrayList<>();
Yes, thats true!
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);

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.