• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to lookup multiple array elements in one go

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

i.e.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That looks like something which you should create a class to handle.
 
Ranch Hand
Posts: 312
MS IE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Luigi Plinge wrote: . . . They are true objects, unlike arrays, . . .

What makes you think arrays are not "true objects"?
 
Get meta with me! What pursues us is our own obsessions! But not this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic