aspose file tools*
The moose likes Java in General and the fly likes How to lookup multiple array elements in one go Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to lookup multiple array elements in one go" Watch "How to lookup multiple array elements in one go" New topic
Author

How to lookup multiple array elements in one go

Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Hi Java Gurus,

I am wondering whether it is possible to reference multiple elements of a single array in one go. Let’s have a look at the example below:

Firstname Surname Age Sex Interest

John Smith 29 M Diving Tennis Football



I can lookup each element in the array using friendshipArray[count]. However, is it possible to refer to more than one in one statement such as all his interests with friendshipArray[5-7].toString()…?

Thanks,

Jack
Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

Here is a "way" to do this:



to explain line 3:
step 1: get copy of array from index 4 to 6 using Arrays.copyOfRange()
step 2: get list view of the above copy using Arrays.asList()
step 3: get toString() value of the above list which will give you: "[Diving,Tennis,Football]"
step 4: replace "[" and "," and "]" with a space. which will give you: " Diving Tennis Football "
step 5: call trim() on this String to get the beginning and trailing white-spaces removed
finally you get "Diving Tennis Football"

you can modify above steps as per your requirements.


Piyush
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

You should prefer Lists to Arrays (Effective Java item 25). Then use the subList method.

i.e.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
That looks like something which you should create a class to handle.
Madhan Sundararajan Devaki
Ranch Hand

Joined: Mar 18, 2011
Posts: 312

Jack Bush wrote: Hi Java Gurus,

I am wondering whether it is possible to reference multiple elements of a single array in one go. Let’s have a look at the example below:

Firstname Surname Age Sex Interest

John Smith 29 M Diving Tennis Football



I can lookup each element in the array using friendshipArray[count]. However, is it possible to refer to more than one in one statement such as all his interests with friendshipArray[5-7].toString()…?

Thanks,

Jack

Another simple way would be, to use indices specific to the locations you want.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
I still think you ought to create a Friend class and pass the String to its constructor.
Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Hi All,

Thank you very much for your valuable suggestions. I decided to use Arrays approach which will allow me to directly use some earlier elements while keeping the remaining ones together, similar to the example provided. Nevertheless, Lists approach should also work just as well. I chose Arrays over Lists just for the sake of familiarity and comforting only. It would be great to know the advantage of using Lists over Arrays though.

Cheers,

Jack
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Here's a little snippet to help with creating lists:You can use it thus:etc.

One advantage of Lists is that they are resizeable so you don't need to know the capacity in advance. They are true objects, unlike arrays, so you can use (non-static) methods on them, subtype them etc. You use them with generics, which allows for better compile-time checking and code re-use. The disadvantages are that their usage syntax is a bit more verbose, and there is a slight performance hit in certain situations, so it helps to be aware of the performance characteristics of different list types.
Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Thank you for having shed light on another approach to achieving the same objective. That is the beauty of Java.

I will endeavour to give it a try when there is a need to use it in the future.

Jack
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Luigi Plinge wrote: . . . They are true objects, unlike arrays, . . .
What makes you think arrays are not "true objects"?
 
 
subject: How to lookup multiple array elements in one go