'Rewrite a C++ vector to java vector
My task is to migrate a code from C++ to Java.
In the C++ there is a vector called vector1 with the following code:
vector1[0].insert(vector1[0].begin(), to_string(i+1)[0]);
How should I write this code to work in java?
Solution 1:[1]
From your code snippets i can guess it a vector of vector of characters because you add a chat from the string, so the code in java will be like this
int i = 1;
Vector<Vector<Character>> vector1 = new Vector<>();
vector1.add(new Vector<>());
vector1.get(0).add(0, String.valueOf(i + 1).charAt(0));
System.out.println(vector1);
If you know the number of vectors it can be Array of vectors of characters like this
int i = 1;
Vector[] vector1 = new Vector[10];
vector1[0] = new Vector<Character>();
vector1[0].add(0, String.valueOf(i + 1).charAt(0));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |