void prepend(ArrayList<String> list, final String first) { list.add(0, first); } // Example use: ArrayList<String> a = new ArrayList<String>(); a.add("one"); a.add("two"); a.add("three"); prepend(a, "Top"); for (int i = 0; i < a.size(); i++) { System.out.println(i + "=" + a.get(i)); }
Programming Tips - Java: Prepend (add at the start) an element to a ArrayList
Date: 2012may3
Language: Java
Q. Java: Prepend (add at the start) an element to a ArrayList
A. Use add() with 0 as the first parameter (the position).