In c++ we can have this kind of vectors:
vector < int > v[20];
I have to create the equivalent in java of a program in c++. I can declare
static ArrayList < ArrayList<Integer > > v;
but it's not quite what I want. I want it to be more intuitive.
I am trying to find a way to easily add elements in my structure.
For example if I have a graph and I am reading a road from a to b, in C++ a simply use v[a].push_back(b).
First of all vector<int> v[20] is not vectors of vector.
Secondly ArrayList could be the equivalent of std::vectors in Java.
The general equivalent of vector.push_back could be like this ie, using generic list:-
List<Integer> x = new ArrayList<Integer>();
x.add(1);
List<Integer>[] v = (List<Integer>[]) Array.newInstance(Integer.class, 20);
{
for (int i = 0; i < v.length; ++i) {
v[i] = new ArrayList<>();
}
}
Sadly, Java is not so concise or well designed here. Array.newInstance serves for all dimesions: int[][][] would be Array.newInstance(int.class, 10, 10, 10). Hence it returns an Object as least denominator, and needs to be cast.
The initial array contains only nulls, and needs an initialisation.
Correction:
Should of course be List.class not Integer.class.
import java.util.Vector;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Vector<Integer>[] anArray = (Vector<Integer>[]) new Vector[100];
for(int i = 0; i < anArray.length; i++)
anArray[i] = new Vector<Integer>();
anArray[2].add(2);//....
}
}
Related
I'd like to assign a set of variables in java as follows:
int n1,n2,n3;
for(int i=1;i<4;i++)
{
n<i> = 5;
}
How can I achieve this in Java?
This is not how you do things in Java. There are no dynamic variables in Java. Java variables have to be declared in the source code1.
Depending on what you are trying to achieve, you should use an array, a List or a Map; e.g.
int n[] = new int[3];
for (int i = 0; i < 3; i++) {
n[i] = 5;
}
List<Integer> n = new ArrayList<Integer>();
for (int i = 1; i < 4; i++) {
n.add(5);
}
Map<String, Integer> n = new HashMap<String, Integer>();
for (int i = 1; i < 4; i++) {
n.put("n" + i, 5);
}
It is possible to use reflection to dynamically refer to variables that have been declared in the source code. However, this only works for variables that are class members (i.e. static and instance fields). It doesn't work for local variables. See #fyr's "quick and dirty" example.
However doing this kind of thing unnecessarily in Java is a bad idea. It is inefficient, the code is more complicated, and since you are relying on runtime checking it is more fragile. And this is not "variables with dynamic names". It is better described as dynamic access to variables with static names.
1 - That statement is slightly inaccurate. If you use BCEL or ASM, you can "declare" the variables in the bytecode file. But don't do it! That way lies madness!
If you want to access the variables some sort of dynamic you may use reflection. However Reflection works not for local variables. It is only applyable for class attributes.
A rough quick and dirty example is this:
public class T {
public Integer n1;
public Integer n2;
public Integer n3;
public void accessAttributes() throws IllegalArgumentException, SecurityException, IllegalAccessException,
NoSuchFieldException {
for (int i = 1; i < 4; i++) {
T.class.getField("n" + i).set(this, 5);
}
}
}
You need to improve this code in various ways it is only an example. This is also not considered to be good code.
What you need is named array. I wanted to write the following code:
int[] n = new int[4];
for(int i=1;i<4;i++)
{
n[i] = 5;
}
You should use List or array instead
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
Or
int[] arr = new int[10];
arr[0]=1;
arr[1]=2;
Or even better
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("n1", 1);
map.put("n2", 2);
//conditionally get
map.get("n1");
Dynamic Variable Names in Java
There is no such thing.
In your case you can use array:
int[] n = new int[3];
for() {
n[i] = 5;
}
For more general (name, value) pairs, use Map<>
You don't. The closest thing you can do is working with Maps to simulate it, or defining your own Objects to deal with.
Try this way:
HashMap<String, Integer> hashMap = new HashMap();
for (int i=1; i<=3; i++) {
hashMap.put("n" + i, 5);
}
I'd like to assign a set of variables in java as follows:
int n1,n2,n3;
for(int i=1;i<4;i++)
{
n<i> = 5;
}
How can I achieve this in Java?
This is not how you do things in Java. There are no dynamic variables in Java. Java variables have to be declared in the source code1.
Depending on what you are trying to achieve, you should use an array, a List or a Map; e.g.
int n[] = new int[3];
for (int i = 0; i < 3; i++) {
n[i] = 5;
}
List<Integer> n = new ArrayList<Integer>();
for (int i = 1; i < 4; i++) {
n.add(5);
}
Map<String, Integer> n = new HashMap<String, Integer>();
for (int i = 1; i < 4; i++) {
n.put("n" + i, 5);
}
It is possible to use reflection to dynamically refer to variables that have been declared in the source code. However, this only works for variables that are class members (i.e. static and instance fields). It doesn't work for local variables. See #fyr's "quick and dirty" example.
However doing this kind of thing unnecessarily in Java is a bad idea. It is inefficient, the code is more complicated, and since you are relying on runtime checking it is more fragile. And this is not "variables with dynamic names". It is better described as dynamic access to variables with static names.
1 - That statement is slightly inaccurate. If you use BCEL or ASM, you can "declare" the variables in the bytecode file. But don't do it! That way lies madness!
If you want to access the variables some sort of dynamic you may use reflection. However Reflection works not for local variables. It is only applyable for class attributes.
A rough quick and dirty example is this:
public class T {
public Integer n1;
public Integer n2;
public Integer n3;
public void accessAttributes() throws IllegalArgumentException, SecurityException, IllegalAccessException,
NoSuchFieldException {
for (int i = 1; i < 4; i++) {
T.class.getField("n" + i).set(this, 5);
}
}
}
You need to improve this code in various ways it is only an example. This is also not considered to be good code.
What you need is named array. I wanted to write the following code:
int[] n = new int[4];
for(int i=1;i<4;i++)
{
n[i] = 5;
}
You should use List or array instead
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
Or
int[] arr = new int[10];
arr[0]=1;
arr[1]=2;
Or even better
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("n1", 1);
map.put("n2", 2);
//conditionally get
map.get("n1");
Dynamic Variable Names in Java
There is no such thing.
In your case you can use array:
int[] n = new int[3];
for() {
n[i] = 5;
}
For more general (name, value) pairs, use Map<>
You don't. The closest thing you can do is working with Maps to simulate it, or defining your own Objects to deal with.
Try this way:
HashMap<String, Integer> hashMap = new HashMap();
for (int i=1; i<=3; i++) {
hashMap.put("n" + i, 5);
}
I'd like to assign a set of variables in java as follows:
int n1,n2,n3;
for(int i=1;i<4;i++)
{
n<i> = 5;
}
How can I achieve this in Java?
This is not how you do things in Java. There are no dynamic variables in Java. Java variables have to be declared in the source code1.
Depending on what you are trying to achieve, you should use an array, a List or a Map; e.g.
int n[] = new int[3];
for (int i = 0; i < 3; i++) {
n[i] = 5;
}
List<Integer> n = new ArrayList<Integer>();
for (int i = 1; i < 4; i++) {
n.add(5);
}
Map<String, Integer> n = new HashMap<String, Integer>();
for (int i = 1; i < 4; i++) {
n.put("n" + i, 5);
}
It is possible to use reflection to dynamically refer to variables that have been declared in the source code. However, this only works for variables that are class members (i.e. static and instance fields). It doesn't work for local variables. See #fyr's "quick and dirty" example.
However doing this kind of thing unnecessarily in Java is a bad idea. It is inefficient, the code is more complicated, and since you are relying on runtime checking it is more fragile. And this is not "variables with dynamic names". It is better described as dynamic access to variables with static names.
1 - That statement is slightly inaccurate. If you use BCEL or ASM, you can "declare" the variables in the bytecode file. But don't do it! That way lies madness!
If you want to access the variables some sort of dynamic you may use reflection. However Reflection works not for local variables. It is only applyable for class attributes.
A rough quick and dirty example is this:
public class T {
public Integer n1;
public Integer n2;
public Integer n3;
public void accessAttributes() throws IllegalArgumentException, SecurityException, IllegalAccessException,
NoSuchFieldException {
for (int i = 1; i < 4; i++) {
T.class.getField("n" + i).set(this, 5);
}
}
}
You need to improve this code in various ways it is only an example. This is also not considered to be good code.
What you need is named array. I wanted to write the following code:
int[] n = new int[4];
for(int i=1;i<4;i++)
{
n[i] = 5;
}
You should use List or array instead
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
Or
int[] arr = new int[10];
arr[0]=1;
arr[1]=2;
Or even better
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("n1", 1);
map.put("n2", 2);
//conditionally get
map.get("n1");
Dynamic Variable Names in Java
There is no such thing.
In your case you can use array:
int[] n = new int[3];
for() {
n[i] = 5;
}
For more general (name, value) pairs, use Map<>
You don't. The closest thing you can do is working with Maps to simulate it, or defining your own Objects to deal with.
Try this way:
HashMap<String, Integer> hashMap = new HashMap();
for (int i=1; i<=3; i++) {
hashMap.put("n" + i, 5);
}
I'd like to assign a set of variables in java as follows:
int n1,n2,n3;
for(int i=1;i<4;i++)
{
n<i> = 5;
}
How can I achieve this in Java?
This is not how you do things in Java. There are no dynamic variables in Java. Java variables have to be declared in the source code1.
Depending on what you are trying to achieve, you should use an array, a List or a Map; e.g.
int n[] = new int[3];
for (int i = 0; i < 3; i++) {
n[i] = 5;
}
List<Integer> n = new ArrayList<Integer>();
for (int i = 1; i < 4; i++) {
n.add(5);
}
Map<String, Integer> n = new HashMap<String, Integer>();
for (int i = 1; i < 4; i++) {
n.put("n" + i, 5);
}
It is possible to use reflection to dynamically refer to variables that have been declared in the source code. However, this only works for variables that are class members (i.e. static and instance fields). It doesn't work for local variables. See #fyr's "quick and dirty" example.
However doing this kind of thing unnecessarily in Java is a bad idea. It is inefficient, the code is more complicated, and since you are relying on runtime checking it is more fragile. And this is not "variables with dynamic names". It is better described as dynamic access to variables with static names.
1 - That statement is slightly inaccurate. If you use BCEL or ASM, you can "declare" the variables in the bytecode file. But don't do it! That way lies madness!
If you want to access the variables some sort of dynamic you may use reflection. However Reflection works not for local variables. It is only applyable for class attributes.
A rough quick and dirty example is this:
public class T {
public Integer n1;
public Integer n2;
public Integer n3;
public void accessAttributes() throws IllegalArgumentException, SecurityException, IllegalAccessException,
NoSuchFieldException {
for (int i = 1; i < 4; i++) {
T.class.getField("n" + i).set(this, 5);
}
}
}
You need to improve this code in various ways it is only an example. This is also not considered to be good code.
What you need is named array. I wanted to write the following code:
int[] n = new int[4];
for(int i=1;i<4;i++)
{
n[i] = 5;
}
You should use List or array instead
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
Or
int[] arr = new int[10];
arr[0]=1;
arr[1]=2;
Or even better
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("n1", 1);
map.put("n2", 2);
//conditionally get
map.get("n1");
Dynamic Variable Names in Java
There is no such thing.
In your case you can use array:
int[] n = new int[3];
for() {
n[i] = 5;
}
For more general (name, value) pairs, use Map<>
You don't. The closest thing you can do is working with Maps to simulate it, or defining your own Objects to deal with.
Try this way:
HashMap<String, Integer> hashMap = new HashMap();
for (int i=1; i<=3; i++) {
hashMap.put("n" + i, 5);
}
I'd like to assign a set of variables in java as follows:
int n1,n2,n3;
for(int i=1;i<4;i++)
{
n<i> = 5;
}
How can I achieve this in Java?
This is not how you do things in Java. There are no dynamic variables in Java. Java variables have to be declared in the source code1.
Depending on what you are trying to achieve, you should use an array, a List or a Map; e.g.
int n[] = new int[3];
for (int i = 0; i < 3; i++) {
n[i] = 5;
}
List<Integer> n = new ArrayList<Integer>();
for (int i = 1; i < 4; i++) {
n.add(5);
}
Map<String, Integer> n = new HashMap<String, Integer>();
for (int i = 1; i < 4; i++) {
n.put("n" + i, 5);
}
It is possible to use reflection to dynamically refer to variables that have been declared in the source code. However, this only works for variables that are class members (i.e. static and instance fields). It doesn't work for local variables. See #fyr's "quick and dirty" example.
However doing this kind of thing unnecessarily in Java is a bad idea. It is inefficient, the code is more complicated, and since you are relying on runtime checking it is more fragile. And this is not "variables with dynamic names". It is better described as dynamic access to variables with static names.
1 - That statement is slightly inaccurate. If you use BCEL or ASM, you can "declare" the variables in the bytecode file. But don't do it! That way lies madness!
If you want to access the variables some sort of dynamic you may use reflection. However Reflection works not for local variables. It is only applyable for class attributes.
A rough quick and dirty example is this:
public class T {
public Integer n1;
public Integer n2;
public Integer n3;
public void accessAttributes() throws IllegalArgumentException, SecurityException, IllegalAccessException,
NoSuchFieldException {
for (int i = 1; i < 4; i++) {
T.class.getField("n" + i).set(this, 5);
}
}
}
You need to improve this code in various ways it is only an example. This is also not considered to be good code.
What you need is named array. I wanted to write the following code:
int[] n = new int[4];
for(int i=1;i<4;i++)
{
n[i] = 5;
}
You should use List or array instead
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
Or
int[] arr = new int[10];
arr[0]=1;
arr[1]=2;
Or even better
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("n1", 1);
map.put("n2", 2);
//conditionally get
map.get("n1");
Dynamic Variable Names in Java
There is no such thing.
In your case you can use array:
int[] n = new int[3];
for() {
n[i] = 5;
}
For more general (name, value) pairs, use Map<>
You don't. The closest thing you can do is working with Maps to simulate it, or defining your own Objects to deal with.
Try this way:
HashMap<String, Integer> hashMap = new HashMap();
for (int i=1; i<=3; i++) {
hashMap.put("n" + i, 5);
}