canonical example by Tabnine
private void usingArrayList() { ArrayList<String> list = new ArrayList<>(Arrays.asList("cat", "cow", "dog")); list.add("fish"); int size = list.size(); // size = 4 list.set(size - 1, "horse"); // replacing the last element to "horse" String removed = list.remove(1); // removed = "cow" String second = list.get(1); // second = "dog" }
ArrayList<String> list = new ArrayList<String>(); list.add("A"); list.add("B"); list.add("C");
class Test1 { public static void main(String[] s) { long st = System.currentTimeMillis(); List<String> l0 = new ArrayList<String>() {{ add("Hello"); add("World!"); }}; List<String> l1 = new ArrayList<String>() {{ add("Hello"); add("World!"); }}; /* snip */ List<String> l999 = new ArrayList<String>() {{ add("Hello"); add("World!"); }}; System.out.println(System.currentTimeMillis() - st); } }
/** * Increases the result vector component at the specified position by the specified delta. */ private void updateResultVector(int position, int delta) { // inflate the vector to contain the given position while (this.resultVector.size() <= position) { this.resultVector.add(0); } // increment the component value final int component = this.resultVector.get(position); this.resultVector.set(position, component + delta); }
@Test public void testGenericListOfArrays() throws MalformedURLException { GenericBean<String> gb = new GenericBean<>(); ArrayList<String[]> list = new ArrayList<>(); list.add(new String[] {"str1", "str2"}); gb.setListOfArrays(list); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setPropertyValue("listOfArrays[0][1]", "str3 "); assertEquals("str3 ", bw.getPropertyValue("listOfArrays[0][1]")); assertEquals("str3 ", gb.getListOfArrays().get(0)[1]); }
/** * Marshal the elements from the given enumeration into an array of the given type. * Enumeration elements must be assignable to the type of the given array. The array * returned will be a different instance than the array given. */ public static <A, E extends A> A[] toArray(Enumeration<E> enumeration, A[] array) { ArrayList<A> elements = new ArrayList<>(); while (enumeration.hasMoreElements()) { elements.add(enumeration.nextElement()); } return elements.toArray(array); }
@Override public void reduce(Iterable<Tuple2<Long, Long>> values, Collector<Tuple2<Long, Long[]>> out) { neighbors.clear(); Long id = 0L; for (Tuple2<Long, Long> n : values) { id = n.f0; neighbors.add(n.f1); } out.collect(new Tuple2<Long, Long[]>(id, neighbors.toArray(new Long[neighbors.size()]))); } }
/** * Returns exception chain starting from top up to root cause. */ public static Throwable[] getExceptionChain(Throwable throwable) { ArrayList<Throwable> list = new ArrayList<>(); list.add(throwable); while ((throwable = throwable.getCause()) != null) { list.add(throwable); } Throwable[] result = new Throwable[list.size()]; return list.toArray(result); }
ArrayList<String> list = new ArrayList<String>(); list.add("one"); list.add("two"); list.add("three"); String listString = ""; for (String s : list) { listString += s + "\t"; } System.out.println(listString);
@Test public void testGenericListOfArraysWithElementConversion() throws MalformedURLException { GenericBean<String> gb = new GenericBean<>(); ArrayList<String[]> list = new ArrayList<>(); list.add(new String[] {"str1", "str2"}); gb.setListOfArrays(list); BeanWrapper bw = new BeanWrapperImpl(gb); bw.registerCustomEditor(String.class, new StringTrimmerEditor(false)); bw.setPropertyValue("listOfArrays[0][1]", "str3 "); assertEquals("str3", bw.getPropertyValue("listOfArrays[0][1]")); assertEquals("str3", gb.getListOfArrays().get(0)[1]); }
@Override protected void verifyResultsWhenReScaling(ListSink sink, int startElementCounter, int endElementCounter) throws Exception { ArrayList<Integer> list = new ArrayList<>(); for (int i = startElementCounter; i <= endElementCounter; i++) { list.add(i); } Collections.sort(sink.values); Assert.assertArrayEquals(list.toArray(), sink.values.toArray()); }