Created
February 10, 2013 22:01
-
-
Save ffbit/4751244 to your computer and use it in GitHub Desktop.
Array and its Derived List in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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