Results 1 to 4 of 4

Thread: array problem

  1. #1
    oceansdepth is offline Member
    Join Date
    Mar 2008
    Posts
    5
    Rep Power
    0

    Default 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.

  2. #2
    oceansdepth is offline Member
    Join Date
    Mar 2008
    Posts
    5
    Rep Power
    0

    Default

    // ************************************************** **************************
    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 05:28 AM.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    When removing an array element, you have to re-arrange the array for the new size. Something like this.

    Java 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.

  4. #4
    oceansdepth is offline Member
    Join Date
    Mar 2008
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Array problem.. help needed please!
    By SCS17 in forum New To Java
    Replies: 3
    Last Post: 03-06-2008, 11:30 PM
  2. Array List Problem
    By khamuruddeen in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 09:10 AM
  3. array problem
    By wats in forum New To Java
    Replies: 1
    Last Post: 12-12-2007, 08:08 AM
  4. Problem with array Copy
    By coco in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 08:46 AM
  5. array problem
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 02:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •