Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
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 04-04-2008, 06:01 AM
Member
 
Join Date: Mar 2008
Posts: 5
oceansdepth is on a distinguished road
array problem
I have this prob for HW:

Consider an array
int [] nums = new int[30];

int numsCount = 0
for (int index = 0; index < 10; index++)
{
nums[index] = 10 * index;
numsCount++;
}
We want to add and delete elements of this array. Initially we loaded 10 elements.

To add an element, we just need to say
nums[numsCount] = 41;
numsCount++;

and that adds an element and ups the number of "real" elements in our array.

Now we want to delete an element, say the 5th element. But we also need to decrease the
numsCount and do something to replace this 5th element with with the last element and zero out
the last element (or not) so that if we look at the array that we are managing and report on
its valid contents, we just get the numsCount number of elements.

So ----

Write a method deleteElement that accepts an array and a number (int) of "true" elements in the array and
a number delnum that is non-negative and less than the number of "true" elements. deleteElement will
delete the delnum element (double check that it is in the proper range) and replace it with the last "real"
element in the array.
To try: set the last element in the array to some default value.

------------------------
ii tried compiling a lot of codes and none of them seem to work or really make sense to me. ANy help is greatly appreciated.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-04-2008, 06:04 AM
Member
 
Join Date: Mar 2008
Posts: 5
oceansdepth is on a distinguished road
// ************************************************** **************************
import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;

public class testhw
{
public static void main (String [] args)
{



int delnum = 0;
int numsCount =0 ;
int[] nums = new int [30];


for (int index = 0; index < 10; index ++)
{
delnum = index;
nums [delnum] = nums [numsCount - 1];
numsCount--;
System.out.println(nums[index]);
}

}

}




---------------
thats the code i'm working on. i know the variables are probably in the wrong place too. and i'm confused on how to delete the 5th element of the array and replace it with the last element of the array. i got a " Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
" error

Last edited by oceansdepth : 04-04-2008 at 06:28 AM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-04-2008, 06:42 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,402
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
When removing an array element, you have to re-arrange the array for the new size. Something like this.

Code:
int[] anIntArray = {1,2,3}; public static void main(String[] args) { System.out.println("Remove value 2."); anIntArray = deleteArrayElement(2); printArray(); } public int[] deleteArrayElement(int size){ int[] newIntArray = new int[anIntArray.length - 1]; return newIntArray; } public void printArray(){ System.out.println("Size of the anIntArray is " + anIntArray.length); for(int i = 0; i < anIntArray.length; i++){ System.out.println("anIntArre[" + i + "] = " + anIntArray[i]); } }
To add the last element in the delete position you have think about the logic.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-05-2008, 04:25 AM
Member
 
Join Date: Mar 2008
Posts: 5
oceansdepth is on a distinguished road
thanks for your help!
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
Array problem.. help needed please! SCS17 New To Java 3 03-07-2008 12:30 AM
Array List Problem khamuruddeen New To Java 1 12-22-2007 10:10 AM
array problem wats New To Java 1 12-12-2007 09:08 AM
Problem with array Copy coco New To Java 1 08-07-2007 09:46 AM
array problem Albert Advanced Java 2 07-01-2007 03:13 AM


All times are GMT +3. The time now is 04:39 AM.


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