Skip to content

Instantly share code, notes, and snippets.

@ffbit
Created February 10, 2013 22:01
Show Gist options
  • Select an option

  • Save ffbit/4751244 to your computer and use it in GitHub Desktop.

Select an option

Save ffbit/4751244 to your computer and use it in GitHub Desktop.
Array and its Derived List in Java
package com.ffbit.collections;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
public class ArrayAndArraysAsListTest {
private List<Integer> list;
private Integer[] array;
@Before
public void setUp() {
array = new Integer[] { 1, 2 };
list = Arrays.asList(array);
}
@Test(expected = UnsupportedOperationException.class)
public void itShouldThrowExceptionWhenModified() throws Exception {
list.add(3);
}
@Test
public void itShouldChangeUnderlyingArray() throws Exception {
list.set(0, 3);
assertThat(array, is(list.toArray(new Integer[] {})));
}
@Test
public void itShouldChangeDerivedList() throws Exception {
array[0] = 3;
assertThat(array, is(list.toArray(new Integer[] {})));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment