• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

How to get values from an arraylist

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I know how to get values from the following array list.

[some string 1, some string 2, some string 3]



I can either loop through it and store these values into strings or get the values by index:


This is when I know how many values are there going to be in an array list.

But if I don't know, how many values are there going to be in an array list, then how can I get them?

For example, if I am trying to get 3 values from an array list, but in that array list there are more than 3 values, then how to handle that situation?

Thanks.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

malik ge wrote:But if I don't know, how many values are there going to be in an array list, then how can I get them?
For example, if I am trying to get 3 values from an array list, but in that array list there are more than 3 values, then how to handle that situation?


That depends entirely on which 3 you want: The first 3? The last 3? 3 from the middle? 3 at random?

You need to be more specific.

Winston
 
malik ge
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for not being specific.

What I meant to say is that if array list's size is unknown. Meaning, I don't know if there are going to be 3 elements or 4 or more than that.
 
Sheriff
Posts: 27235
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It isn't possible for the size of an array list to be unknown. There's a method which tells you its size... you could look it up in the API documentation.
 
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use the size() of List which will return the ArrayList's size.. and then iterate through that number which you get.
or Else you an Enhance For loop..



HIH
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ishan Pandya is correct, either you can use normal for loop or foreach to iterate all the elements of the arraylist.

Here is the example for your reference How to Use ( Java ) For Loop with ArrayList
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic