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?

Recommended Answers

All 11 Replies

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

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

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?

What other way you looking for? What for?

Well firstly, does the method I listed work?

It does work like that as long you keep it to same type.

The only other way I know of would be to loop through the array:

for(int i=0;i<sourcearr.length;i++){
    argarr[i]=sourcearr[i];
}

This is extra code and (a lot) extra computation, but it becomes useful if you're sifting through the data at the same time.
(e.g.)

for(int i=0;i<sourcearr.length;i++){
    if(foo) {argarr[i]=sourcearr[i];}
//do something and use that value instead.
    else {argarr[i]=bar(sourcearr[i]);}
}

But that is the *only* time I know of where it's useful, so stay in the habit of newarr=oldarr;

Ok cool, thanks very much.

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 array argument_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 the example_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];
}

Oh! good call... I stand corrected.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.