• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to access data out of an array list  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all! I have been reading this forum and site for awhile now and it's been really helpful! Thank you in advance for any help. I am curious if this is possible. Pretending there is an array of names and two methods that return a first and last name.. Say you have a loop like this:



Is it possible to grab the index number of i and use it as a choice to display more details? So for example say my code prints out this:

0 First name: Harry
1 Last name: Sam

Basically I am looking for a way of accessing Harry out of the arraylist and getting more information and I am stuck on the syntax for that. I figured a first start would be printing the index out so we know which number corresponds to which contact. From there I am not sure how I would grab the index as user input and display it...A push or idea in the right direction would be most helpful!

I could be going about it the wrong way from the start which might be my problem.

Thank you!
 
Master Rancher
Posts: 2045
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jill,

unofficial: welcome! (the official welcome will come from the bartender/marshall/sheriff in charge),
thanks for using the code tags!

Well, in your example, and assuming that your array is an ArrayList<String>, the 'i' that you use
in your loop is already the 'index'!
So:

array.get(i) will give you the string that is located at index i, like in:

Have a look at the Oracle API of ArrayList<E>.

But if you want to work with a firstName and a lastName, then maybe a construct like

and then using

will give you much more comfort.

Give it a try!

Greetings,
Piet

Edit: reading your question again, I think that you already did what I am suggesting.
In my example, if you have an index i, then you would also do something like:

You write that if you have, say, this firstName, you want to retrieve more information.

If so, then please explain what this extra information is, and how you store that extra information.
Maybe by expanding the 'FullName' class with extra information?
 
Jill Robins
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Piet for the answer and welcome! Sorry I was a little vague, I wasn't sure how much was needed and was thinking in theory. Yes so I do have an Array that references a class of contacts. It looks like this and is in my main class:



and I have a separate class of contacts that I store in my array from user input:



and I used that loop




to access my getters in the Contacts class to only display the first and last name. They look like this:





Example print:

Index Number: 0 First name: Harry Last Name: Smith
Index Number: 1 First name: Sally Last Name: Smith

From there, I wanted to be able to choose an index using user input (using 0, 1, 2, ect) to display more information about that contact in the ArrayList. After I can figure out how to access it using user input, I should be able to use the rest of my methods, getEmail(), getAddress() ect to display everything to the user. I hope that makes sense, Thanks for your help!
 
Piet Souris
Master Rancher
Posts: 2045
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first of all, in your current for() - loop you could make life a bit more comfortable
if you first retrieve a Contacs from your array (assuming that array is an ArrayList<Contacts>)

But you could of course print the complete information just as well, eliminating the need to ask
for the specific information.

To get user input from the command lne, you could use a BufferedReader, but usually a Scanner
is used for that. Look at the API of the Scanner class and you will see a lot of handy input-methods.

Another possibility is to ask the user for a name (first or last), using again a Scanner, and then search
your array, in a for - loop, which Contacts from your array has that name, and then display
the info.

So, have a look at the Scanner class in the Oracle API.

Greetings,
Piet
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote: . . . unofficial: welcome! (the official welcome will come from the bartender/marshall/sheriff in charge),
. . .
What's official or unofficial about a welcome?

Welcome again, officially, unofficially, whatever, but warmly

Don't call your List array; that will cause no end of confusion if you come back to it next year. You will really believe yourself when you say you have an Array. You haven't;. You have a List. It might have an array inside, but that is not what you have here.
Encapsulate that Contacts class, so you initialise all those fields in the constructor (as Piet has already shown you) and give it get methods, and give all its fields private access. The methods should ideally be called getLastName() etc., to match the field names.

Do not try to put Strings into that List 5 at a time. That is very iffy design and can lead to all sorts of errors later on.
 
Jill Robins
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to both of you for your suggestions and help!! I am going to try to come up with something better based on your suggestions!
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Piet Souris
Master Rancher
Posts: 2045
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I second that!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!