How to move left ArrayList

I use ArrayList to store object history. Each new object that I add using the .add method, for example:

if(event.getAction() == MotionEvent.ACTION_UP) { if(currentWord != null) { wordHist.add(currentWord); } if(wordHist.size() > WORDHIST_MAX_COUNT) { wordHist.remove(0); } } 

However, I do not want this to grow indefinitely, but be limited to a certain value. If it reaches this maximum value, I want to delete the oldest object (index 0), and the rest will remain shifted, so the previous index 1 is now index 0, etc.

How can I do that?

thanks

+4
source share
7 answers

ArrayList is not really a good choice in this case , but you can do this by calling the remove (0) method. But if you want to do it efficiently, a linked list is better.

(edited to make it clear that LinkedList is usually no better than ArrayList, but only in this case)

+2
source

If it reaches this maximum value, I want to delete the oldest object (index 0)

Then do wordHist.remove(0) . This will remove the item with index 0.

To be precise:

 wordHist.add(new Word("hello")); if (wordHist.size() > MAX_SIZE) wordHist.remove(0); 

At the same time, like user658991, you should know that this is a linear operation, i.e. time is proportional to the number of items in the list.

You can do this all the time using the LinkedList add and removeFirst .

Another option is to wrap an array or ArrayList in a class called something like CircularArrayList . In circular list structures, you will override the oldest item when adding a new one.

Edit:

Your code works fine:

 import java.util.*; class Test { static int WORDHIST_MAX_COUNT = 3; static List<String> wordHist = new ArrayList<String>(); public static void add(String currentWord) { // VERBATIM COPY OF YOUR CODE if (true/*event.getAction() == MotionEvent.ACTION_UP*/) { if(currentWord != null) { wordHist.add(currentWord); } if(wordHist.size() > WORDHIST_MAX_COUNT) { wordHist.remove(0); } } } public static void main(String[] args) { add("a"); add("b"); add("c"); for (int i = 0; i < wordHist.size(); i++) System.out.printf("i: %d, word: %s%n", i, wordHist.get(i)); System.out.println(); add("d"); for (int i = 0; i < wordHist.size(); i++) System.out.printf("i: %d, word: %s%n", i, wordHist.get(i)); } } 

Print

 i: 0, word: a i: 1, word: b i: 2, word: c i: 0, word: b <-- b is now at index 0. i: 1, word: c i: 2, word: d 
+1
source

Use the remove( ) method.

Using remove(0) will remove the item from the 0th index.

0
source

U can use list.remove (index) // here the index is '0', this internally shifts the rest of the array up. An alternative solution would be to use a queue or deactivation.

0
source

One simple implementation of what Op De Cirkel has proposed

 import java.util.ArrayList; import java.util.List; public class SimpleCircularHistory { private int sizeLimit, start = 0, end = 0; boolean empty = false; private List<String> history; public SimpleCircularHistory(int sizeLimit) { this.sizeLimit = sizeLimit; history = new ArrayList<String>(sizeLimit); } public void add(String state){ empty = false; end = (end + 1) % sizeLimit; if(history.size() < sizeLimit){ history.add(state); }else { history.set(end, state); start = (end + 1) % sizeLimit; } } public String rollBack(){ if(empty){ // Empty return null; }else { String state = history.get(end); if(start == end){ empty = true; }else { end = (end + sizeLimit - 1) % sizeLimit; } return state; } } public void print(){ if(empty){ System.out.println("Empty"); }else { for(int i = start;; i = (i + 1) % sizeLimit){ System.out.println(history.get(i)); if(i == end) break; } System.out.println(); } } public static void main(String[] args) { SimpleCircularHistory h = new SimpleCircularHistory(3); h.add("a"); h.add("b"); h.add("c"); h.add("d"); h.add("e"); h.add("f"); h.print(); h.add("X"); h.add("Y"); h.rollBack(); h.rollBack(); h.print(); h.add("t"); h.add("v"); h.add("w"); h.print(); h.rollBack(); h.rollBack(); h.rollBack(); h.print(); h.rollBack(); h.print(); } } 

This will print:

 d e f f t v w Empty Empty 
0
source

Yes, I also noticed this behavior on adroid lists. This is REALLY annoying.

Anyway, there is a way around it if I do not mind creating / destroying the object and the final garbage collection (NEVER do this in an onDraw surface or something like that).

I basically have two int tracking; one for placing a new object and one for deleting it:

  int trackInt = 0; int removeInt = 0; //and then, in the method/class you use this: Object newobject = new Object(); //add to list objectList.add(trackInt, newobject); trackInt++; if (bugList.size() > 20) //20 is the max number of object you want, ie the maximum size of the list { objectList.remove(removeInt); trackInt = removeInt; removeInt++; if (removeInt > 19) //remember, the list is zero indexed! { removeInt = 0; } } 
0
source

All Articles