Java examples for Collection Framework:Array Element
shift element in ArrayList
/*/*w ww . ja va2s .c o m*/ Holyoke Framework: library for GUI-based database applications This file Copyright (c) 2006-2008 by Robert Fischer This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ //package com.java2s; import java.util.*; public class Main { public static void shift(ArrayList al, int ix, int n, int shift) { // int oldsize = al.size(); // int lastix = ix + n + shift; // last index (+1) of last item being shifted if (shift > 0) { // while (al.size() < lastix) al.add(null); // potentially add... for (int i = n - 1; i >= 0; --i) al.set(ix + i + shift, al.get(ix + i)); } else { for (int i = 0; i < n; ++i) al.set(ix + i + shift, al.get(ix + i)); } } public static void shift(ArrayList al, int ix, int shift) { int n = al.size() - ix; if (shift > 0) n -= shift; shift(al, ix, n, shift); } }