Thursday, 15 January 2015

java - Remove items from ArrayList with certain value -


I've created a list of items and added people to:

  ArrayList & Lt; Person & gt; Pips = new arreelist & lt; Person & gt; (); Peeps.add (new person ("112", "John", "Smith")); Peeps.add (new person ("516", "Jane", "Smith")); Peeps.add (new person ("114", "John", "Do"));  

I am trying to figure out how to remove the person from the list by ID number. So if I want to remove the person from ID number 114, but no longer, where it fell into the list, how would I be?

If you are going to use an arrelist, then the only way to go through the entire list is to go Seeing each person, seeing this, his ID number is 114. For larger datasets, it is not going to be efficient and should be avoided.

If you can change your data structure, then some sort will be better (usually a good choice). You can have ID number as a "key" and after that you can associate it with each person. You can later query the map by the key. You can only have one value in the form of a key in the election, so you can not say both the name and the ID number key

EDIT:
To use an ArrayList one more The efficient way would be to keep it sorted by the ID number, then you can use something to access the elements quickly based on ID number. The choice is that it is expensive to remove / insert from a sorted array because the larger element is to be moved. So if you are going to make relatively few changes compared to the reading number, it can be viable


No comments:

Post a Comment