aspose file tools*
The moose likes Beginning Java and the fly likes How to get values from an arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to get values from an arraylist" Watch "How to get values from an arraylist" New topic
Author

How to get values from an arraylist

malik ge
Ranch Hand

Joined: May 13, 2011
Posts: 69
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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
malik ge
Ranch Hand

Joined: May 13, 2011
Posts: 69
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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

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.
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 223

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


OCPJP
Naveen Bachu
Greenhorn

Joined: Feb 12, 2013
Posts: 4
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get values from an arraylist