Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-06-2007, 02:13 AM
Member
 
Join Date: Nov 2007
Posts: 7
nalinda is on a distinguished road
deleting elements
how do i delete an element from an array? Give me the simplest way. And how do i do it without leaving gaps in the array?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-06-2007, 02:16 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-06-2007, 02:42 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,146
hardwired is on a distinguished road
Here's a couple of simple ways.
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; }
staykovmarin's way using collections is even easier. The designers say that collections run faster than arrays. One advantage of using arrays is that you don't have to deal with generics.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting All rows in the JTable surot New To Java 1 04-16-2008 11:44 AM
Deleting a File that is opened ravian Advanced Java 6 01-30-2008 03:05 PM
Deleting files after filtering the extensions Java Tip Java Tips 0 01-25-2008 08:00 PM
Deleting an empty directory Java Tip Java Tips 0 01-13-2008 08:17 AM
Deleting certain image pixels.. Brightside New To Java 1 05-22-2007 10:21 AM


All times are GMT +3. The time now is 06:18 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org