Skip to content
Snippets Groups Projects
Commit 9c056fca authored by Johannes Schindelin's avatar Johannes Schindelin Committed by Shawn O. Pearce
Browse files

Add set to IntList


Some applications may wish to modify an int list.

Bug: 291083
Eclipse-CQ: 3559
Change-Id: Iea871443ec661230aec92397229f1eda6c74216f
Signed-off-by: default avatarJohannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: default avatarShawn O. Pearce <spearce@spearce.org>
parent 6e1571d5
No related branches found
No related tags found
No related merge requests found
...@@ -150,6 +150,27 @@ public void testClear() { ...@@ -150,6 +150,27 @@ public void testClear() {
} }
} }
public void testSet() {
final IntList i = new IntList();
i.add(1);
assertEquals(1, i.size());
assertEquals(1, i.get(0));
i.set(0, 5);
assertEquals(5, i.get(0));
try {
i.set(5, 5);
fail("accepted set of 5 beyond end of list");
} catch (ArrayIndexOutOfBoundsException e){
assertTrue(true);
}
i.set(1, 2);
assertEquals(2, i.size());
assertEquals(2, i.get(1));
}
public void testToString() { public void testToString() {
final IntList i = new IntList(); final IntList i = new IntList();
i.add(1); i.add(1);
......
/* /*
* Copyright (C) 2008, Google Inc. * Copyright (C) 2008, Google Inc.
* Copyright (C) 2009, Johannes Schindelin <johannes.schindelin@gmx.de>
* and other copyright owners as documented in the project's IP log. * and other copyright owners as documented in the project's IP log.
* *
* This program and the accompanying materials are made available * This program and the accompanying materials are made available
...@@ -99,6 +100,23 @@ public void add(final int n) { ...@@ -99,6 +100,23 @@ public void add(final int n) {
entries[count++] = n; entries[count++] = n;
} }
/**
* Assign an entry in the list.
*
* @param index
* index to set, must be in the range [0, {@link #size()}).
* @param n
* value to store at the position.
*/
public void set(final int index, final int n) {
if (count < index)
throw new ArrayIndexOutOfBoundsException(index);
else if (count == index)
add(n);
else
entries[index] = n;
}
/** /**
* Pad the list with entries. * Pad the list with entries.
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment