| | |
Some questions about arrays
![]() |
•
•
Join Date: Dec 2007
Posts: 74
Reputation:
Solved Threads: 2
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
Is this the only way to do something like this?
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
Java Syntax (Toggle Plain Text)
System.out.println("The data inside "+string_example[1]+" is "+example_int[1];
Is this the only way to do something like this?
Last edited by redZERO; Oct 5th, 2008 at 5:12 am.
Create a class like this:
And:
In that way you only have 1 array of items.
Another way is to use Hashtable
Java Syntax (Toggle Plain Text)
class DataClass { public int intValue; public String strValue; public DataClass() { } }
And:
Java Syntax (Toggle Plain Text)
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
Check out my New Bike at my Public Profile at the "About Me" tab
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/tutor...ons/index.html
You can read about the various Collections and their usage here: http://java.sun.com/docs/books/tutor...ons/index.html
Last edited by Ezzaral; Oct 5th, 2008 at 10:36 am.
What other way you looking for? What for?
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
It does work like that as long you keep it to same type.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Feb 2008
Posts: 16
Reputation:
Solved Threads: 1
The only other way I know of would be to loop through the array:
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.)
But that is the *only* time I know of where it's useful, so stay in the habit of
Java Syntax (Toggle Plain Text)
for(int i=0;i<sourcearr.length;i++){ argarr[i]=sourcearr[i]; }
(e.g.)
java Syntax (Toggle Plain Text)
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]);} }
newarr=oldarr; Actually this way is a bad idea:
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:
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:
Java Syntax (Toggle Plain Text)
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:
Java Syntax (Toggle Plain Text)
example_array = argument_array;
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:
Java Syntax (Toggle Plain Text)
String [] example_array = new String[argument_array.length]; for (int i=0;i<example_array.length;i++) { example_array[i] = argument_array[i]; }
Check out my New Bike at my Public Profile at the "About Me" tab
![]() |
Similar Threads
- Conversion from variables to arrays (C++)
- arrays in java!!! (Java)
- Storing dynamic form values in Arrays for display & insert (PHP)
- Sorting arrays of pointers with function? (C)
- Help with Arrays needed! (C++)
- Preparing for an interview and need some questions answered (C)
Other Threads in the Java Forum
- Previous Thread: Connection Pools
- Next Thread: JTAPI
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer html ide image inetaddress integer integration intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program programming project recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows






