Some questions about arrays
Hi
I'd like to use an array to store values of particular things. I would like names to correspond to each value in the array. What I was thinking was, should I perhaps create 2 arrays: String[] and int[]. Then, when I call data from say example_int[1] I call the same from string_example[1] and just make them correspond?
So i could say something like
System.out.println("The data inside "+string_example[1]+" is "+example_int[1];
Is this the only way to do something like this?
redZERO
Junior Poster in Training
82 posts since Dec 2007
Reputation Points: 10
Solved Threads: 2
Create a class like this:
class DataClass {
public int intValue;
public String strValue;
public DataClass() {
}
}
And:
DataClass arr [] = new DataClass[2];
arr[0] = new DataClass();
arr[0].intValue = 0;
arr[0].strValue = "value 0";
arr[1] = new DataClass();
arr[1].intValue = 1;
arr[1].strValue = "value 1";.
for (int i=0;i<arr.length;i++) {
System.out.println(arr[i].intValue +" "+arr[i].strValue );
}
In that way you only have 1 array of items.
Another way is to use Hashtable
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
Your exact usage is a little bit vague, but it sounds like you want to use a Map structure. The Hashtable that JavaAddict mentioned is one implementation of that, though it's an older one and HashMap is generally preferred now. There are a couple of other implementations as well that offer specific features.
You can read about the various Collections and their usage here: http://java.sun.com/docs/books/tutorial/collections/index.html
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
Thanks,
Another question about arrays is whether I can take all the values from one array and put them in another one. In the same order obviously.
So can I say
example_array =
argument_array;
Is there another way to do this?
redZERO
Junior Poster in Training
82 posts since Dec 2007
Reputation Points: 10
Solved Threads: 2
What other way you looking for? What for?
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
Well firstly, does the method I listed work?
redZERO
Junior Poster in Training
82 posts since Dec 2007
Reputation Points: 10
Solved Threads: 2
It does work like that as long you keep it to same type.
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
Ok cool, thanks very much.
redZERO
Junior Poster in Training
82 posts since Dec 2007
Reputation Points: 10
Solved Threads: 2
Actually this way is a bad idea:
example_array = argument_array;
You don't copy the elements of one array to the other, you say that the 'example_array' is the 'same' as the 'argument_array'. They both 'look' at the same object in memory, so if change the element in one array the other array will change also.
Suppose you have an original arrayargument_array and you want to make some changes to it, but keep a copy of the original. If you do this:
example_array = argument_array;
and try to change theexample_array and keep the argument_array intact, it will not work. When you try to change the example_array, the argument_array will change as well.
So I would suggest, always use the for-loop way to copy arrays because in that way you create a new array and changes made to one of them will be made to other:
String [] example_array = new String[argument_array.length];
for (int i=0;i<example_array.length;i++) {
example_array[i] = argument_array[i];
}
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847