• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting elements from an array list  RSS feed

 
David Ausere
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an Array List called Customer which is filled with customers and the first field is the ID.

I want to be able to get the value of the last number is the ID field.  How would I go about doing that?
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know a quick way to do this with ArrayList, other than looping through the whole list, but if you can use LinkedList, there is a getLast() method.
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Ausere wrote:I want to be able to get the value of the last number is the ID field.

I don't understand this bit. Can you rephrase it? Or ask it a little differently? Thanks
 
David Ausere
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my array lists.
ArrayList<Customers> CustList = new ArrayList<Customers>();

Here are the elements that I am putting in that list

Customer customer = new Customer (ID, fname, lname, email, adresss, phone);

As an I am inputing customers into the list I want to be able to see what the last ID of the last customer so that when I put a new customer in I am not repeating the customer ID and that the ID's are staying in order.

I have tried to logically think it out, but I am not a codemooser.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us an example of what you are looking for.
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With your current design you'll have to iterate through the whole list, inspect the ID of each Customer, and remember the highest number you found.

It's not ideal, especially if you have to do it often and the number of Customers is large. Perhaps there's a better solution? It's worth thinking about that.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see what the last ID of the last customer

If the Customer objects are being added at the end of the list, then the ID for the last customer that was added would  be available from the  customer object at the end of the list.  The ArrayList class has a method that could be used to get the index to the last element in the list.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are all sorts of ways to implement the specification
A different number each time.
As you know, there are only 2³² different ints and 2⁶⁴ different longs, so you will run out eventually, but you will probably run out of memory (or patience) before you run out of numbers. You can create a Set of numbers and randomly choose a number and keep going until you find one not already in the Set. That is one way to do it. It isn't easy, and you may find your performance gets very slow as the Set fills up What follows is another way to implement it.If you don't mind the first ID being 0, you can get 2³¹ different numbers out of that class before they turn negative . . . and they will be in order. Now you simply need the last entry in the List.
Another way to link numbers to customers would be a Map. There are various kinds of Map which might return the numbers in order or out of order. Read about Maps in the Java™ Tutorials.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!