954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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
Team Colleague
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
Moderator
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
Moderator
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
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

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;

cknapp
Newbie Poster
16 posts since Feb 2008
Reputation Points: 10
Solved Threads: 1
 

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
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

Oh! good call... I stand corrected.

cknapp
Newbie Poster
16 posts since Feb 2008
Reputation Points: 10
Solved Threads: 1
 

To copy single dimension arrays, use System.arraycopy() or Arrays.copyOf() .

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You