Skip to content

Instantly share code, notes, and snippets.

@arouel
Created October 18, 2011 21:01
Show Gist options
  • Save arouel/1296708 to your computer and use it in GitHub Desktop.
Save arouel/1296708 to your computer and use it in GitHub Desktop.
Examples for an initialized list to replace element on a particular position.
package de.rouel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import junit.framework.Assert;
import org.junit.Test;
/**
* How can you setup a list with one or more elements at a particular position (index), without having to add elements
* to these position with <code>List.add(T element)</code>?
*
* @see java.util.List#add(Object)
*
* @author André Rouél
*/
public class ListTest {
/**
* This test shows an easier initialization to replace elements on an expected position in a <code>List</code>.
*/
@Test
public void testGetByIndexInitiallyFilled() {
final String[] l = new String[11];
final List<String> list = new ArrayList<String>(Arrays.asList(l));
// adds an element for index 3, and now you can replace it with set(idx, string)
list.set(3, "test on index 3");
// adds an element for index 10, and now you can replace it with set(idx, string)
list.set(10, "test on index 10");
Assert.assertEquals("test on index 10", list.get(10));
Assert.assertEquals("test on index 3", list.get(3));
Assert.assertNull(list.get(0));
Assert.assertNull(list.get(2));
Assert.assertNull(list.get(7));
list.remove(3);
Assert.assertNull(list.remove(3));
}
/**
* Of course an empty list throws an <code>IndexOutOfBoundsException</code> if you want to get an element of an
* non-existent position.
*/
@Test(expected = IndexOutOfBoundsException.class)
public void testGetByIndexOnEmptyList() {
final List<String> list = new ArrayList<String>();
list.get(3);
}
/**
* This way you get an initialized list of strings with 11 <code>null</code>-elements.
*/
@Test
public void testGetByIndexOnInitializedList() {
final String[] l = new String[11];
final List<String> list = new ArrayList<String>(Arrays.asList(l));
Assert.assertNull(list.get(10));
}
/**
* This test shows an complicated way to replace elements on an expected position in a <code>List</code>.<br>
* <br>
* <b>Attention</b>: This way is not recommended!
*/
@Test
public void testGetByIndexOnManuallyFilledList() {
final List<String> list = new ArrayList<String>(10);
list.add(null);
list.add(null);
list.add(null);
// adds an element for index 3, and now you can replace it with set(idx, string)
list.add(null);
list.set(3, "test on index 3");
list.add(null);
list.add(null);
list.add(null);
list.add(null);
list.add(null);
list.add(null);
// adds an element for index 10, and now you can replace it with set(idx, string)
list.add(null);
list.set(10, "test on index 10");
Assert.assertEquals("test on index 10", list.get(10));
Assert.assertEquals("test on index 3", list.get(3));
Assert.assertNull(list.get(0));
Assert.assertNull(list.get(2));
Assert.assertNull(list.get(7));
list.remove(3);
Assert.assertNull(list.remove(3));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment