# array problem

• 04-04-2008, 04:01 AM
oceansdepth
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.
• 04-04-2008, 04:04 AM
oceansdepth
// ************************************************** **************************
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
• 04-04-2008, 04:42 AM
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.
• 04-05-2008, 02:25 AM
oceansdepth