Some questions about arrays

Reply

Join Date: Dec 2007
Posts: 74
Reputation: redZERO is an unknown quantity at this point 
Solved Threads: 2
redZERO redZERO is offline Offline
Junior Poster in Training

Some questions about arrays

 
0
  #1
Oct 5th, 2008
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

  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,658
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 224
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Some questions about arrays

 
0
  #2
Oct 5th, 2008
Create a class like this:

  1. class DataClass {
  2. public int intValue;
  3. public String strValue;
  4.  
  5. public DataClass() {
  6.  
  7. }
  8. }

And:

  1. DataClass arr [] = new DataClass[2];
  2.  
  3. arr[0] = new DataClass();
  4. arr[0].intValue = 0;
  5. arr[0].strValue = "value 0";
  6.  
  7. arr[1] = new DataClass();
  8. arr[1].intValue = 1;
  9. arr[1].strValue = "value 1";.
  10.  
  11. for (int i=0;i<arr.length;i++) {
  12. System.out.println(arr[i].intValue +" "+arr[i].strValue );
  13. }

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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,439
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Some questions about arrays

 
0
  #3
Oct 5th, 2008
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
Last edited by Ezzaral; Oct 5th, 2008 at 10:36 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 74
Reputation: redZERO is an unknown quantity at this point 
Solved Threads: 2
redZERO redZERO is offline Offline
Junior Poster in Training

Re: Some questions about arrays

 
0
  #4
Oct 5th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,190
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 483
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Some questions about arrays

 
-1
  #5
Oct 5th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 74
Reputation: redZERO is an unknown quantity at this point 
Solved Threads: 2
redZERO redZERO is offline Offline
Junior Poster in Training

Re: Some questions about arrays

 
0
  #6
Oct 5th, 2008
Well firstly, does the method I listed work?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,190
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 483
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Some questions about arrays

 
-1
  #7
Oct 5th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 16
Reputation: cknapp is an unknown quantity at this point 
Solved Threads: 1
cknapp cknapp is offline Offline
Newbie Poster

Re: Some questions about arrays

 
0
  #8
Oct 5th, 2008
The only other way I know of would be to loop through the array:
  1. for(int i=0;i<sourcearr.length;i++){
  2. argarr[i]=sourcearr[i];
  3. }
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.)
  1. for(int i=0;i<sourcearr.length;i++){
  2. if(foo) {argarr[i]=sourcearr[i];}
  3. //do something and use that value instead.
  4. else {argarr[i]=bar(sourcearr[i]);}
  5. }
But that is the *only* time I know of where it's useful, so stay in the habit of newarr=oldarr;
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 74
Reputation: redZERO is an unknown quantity at this point 
Solved Threads: 2
redZERO redZERO is offline Offline
Junior Poster in Training

Re: Some questions about arrays

 
0
  #9
Oct 5th, 2008
Ok cool, thanks very much.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,658
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 224
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Some questions about arrays

 
0
  #10
Oct 6th, 2008
Actually this way is a bad idea:

  1. 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:
  1. 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:

  1. String [] example_array = new String[argument_array.length];
  2. for (int i=0;i<example_array.length;i++) {
  3. example_array[i] = argument_array[i];
  4. }
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC