• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to lookup multiple array elements in one go

 
Jack Bush
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should prefer Lists to Arrays (Effective Java item 25). Then use the subList method.

i.e.
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks like something which you should create a class to handle.
 
Madhan Sundararajan Devaki
Ranch Hand
Posts: 312
Java MS IE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still think you ought to create a Friend class and pass the String to its constructor.
 
Jack Bush
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48382
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luigi Plinge wrote: . . . They are true objects, unlike arrays, . . .
What makes you think arrays are not "true objects"?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic