Results 1 to 3 of 3
Thread: deleting elements
- 12-06-2007, 01:13 AM #1
Member
- Join Date
- Nov 2007
- Posts
- 7
- Rep Power
- 0
- 12-06-2007, 01:16 AM #2
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
You cant, Arrays are a set size, you can set the contents to null, but then you will be left with "gaps".
Use HashMap, ArrayList, or one of the million other collection classes.
- 12-06-2007, 01:42 AM #3
Here's a couple of simple ways.
Java Code:public static void main(String[] args) { String[] items = { "cat", "dog", "horse", "fish", "cow" }; System.out.println("items = " + Arrays.toString(items)); items = removeItemAt(2, items); System.out.println("items = " + Arrays.toString(items)); items = remove(3, items); System.out.println("items = " + Arrays.toString(items)); } private static String[] removeItemAt(int index, String[] array) { String[] retVal = new String[array.length-1]; for(int j = 0, k = 0; j < array.length; j++) { if(j == index) continue; retVal[k++] = array[j]; } return retVal; } private static String[] remove(int index, String[] array) { int len = array.length; String[] retVal = new String[len-1]; System.arraycopy(array, 0, retVal, 0, index); System.arraycopy(array, index+1, retVal, index, len-index-1); return retVal; }
Similar Threads
-
Deleting All rows in the JTable
By surot in forum New To JavaReplies: 1Last Post: 04-16-2008, 10:44 AM -
Deleting a File that is opened
By ravian in forum Advanced JavaReplies: 6Last Post: 01-30-2008, 02:05 PM -
Deleting files after filtering the extensions
By Java Tip in forum Java TipReplies: 0Last Post: 01-25-2008, 07:00 PM -
Deleting an empty directory
By Java Tip in forum Java TipReplies: 0Last Post: 01-13-2008, 07:17 AM -
Deleting certain image pixels..
By Brightside in forum New To JavaReplies: 1Last Post: 05-22-2007, 09:21 AM
Bookmarks