JAVA, please help, urgent

Discussion in 'OT Technology' started by nietsni3, Mar 2, 2005.

  1. nietsni3

    nietsni3 Guest

    ok, i will be given an array of int value called inputArray, and an int value called removeItem. My job is to write a method that searchs for all the entries in inputArray that = removeItem, and remove them completely from the array(which means the length of the array has to be shorter everytime a removeItem is found and deleted). and i am NOT allowed to create a new array (which means i am not allowed to cheat by creating a new and shorter array and fill it with the entries of inputArray that are not equal removeItem), not allowed to use class Vector, and the method has to be void method,

    searching and replacing the entries in the array is easy, i can do them by myself. but the problem is after that, the inputArray {x, removeItem, x , x, removeItem, removeItem, x, x, x, removeItem, removeItem} is gonna become {x, x, x, x, x, x , 0, 0, 0, 0, 0} with 0s stand for the removeItems after being deleted. and i dont know how to cut off the tail of 0's at the end from the array to make the it shorter.

    Please help. i have to submit this by the end of the day
    thanks alot
     
    Last edited by a moderator: Mar 2, 2005
  2. beez

    beez New Member

    Joined:
    Jun 3, 2004
    Messages:
    19,143
    Likes Received:
    0
    Location:
    Queens
    So are you allowed to use the Vector class or aren't you? Your message isn't very clear.
     
  3. nietsni3

    nietsni3 Guest

    sorry.
    no Vector class. the prof said that i have to do this manually so that i would appreciate the Vector class
     
  4. beez

    beez New Member

    Joined:
    Jun 3, 2004
    Messages:
    19,143
    Likes Received:
    0
    Location:
    Queens
    I think you might have misunderstood the assignment. Arrays in Java cannot be resized dynamically. That is, they are of fixed length. You can place a restriction on the range of elements you can access in an array by using a separate counter variable that reflects the number of elements in the array after you remove the removeItem elements, but you can't actually change the size of the array. You should check out java.sun.com for more information.

    http://java.sun.com/docs/books/tutorial/java/data/arraysummary.html
     
  5. nietsni3

    nietsni3 Guest

    that line is confusing. i dont know if that was a statement or a request of the problem. perhaps I misunderstood it (as a request) like you said.

    one more question: can i use null for int value? because i really want to replace the removeItem in the inputArray with a "null" value, not 0 because the removeItem may happen to be 0. But seems like int data type doesnt have null value like char or string. the compiler reported error when i try to assign a null value onto an int variable:
    D:\HW\csci 112\Lab4\Lab4.java:76: incompatible types
    found : <nulltype>
    required: int
    if (j == length - 1) inputArray[j] = null;
    ^
    1 error

    is there any equivalent substitution for "null" in the int data type?

    tahnks
     
    Last edited by a moderator: Mar 3, 2005
  6. JayDee07

    JayDee07 New Member

    Joined:
    Aug 27, 2004
    Messages:
    794
    Likes Received:
    0
    maybe your professor meant arraylist, not array?
     
  7. nietsni3

    nietsni3 Guest

    i am afraid not cuz we have not learned arraylist yet
     
  8. aphoric

    aphoric Even if god did exist, it would be necessary to ab

    Joined:
    Aug 29, 2003
    Messages:
    918
    Likes Received:
    0
    Location:
    Leaving Afghanistan
    Create a custom array class that contains an array of artificially large size and a class variable that keeps track of the size of the array that is actually filled by valid values. an int cannot be null, it is 0 (zero), and Integer can be null. (not considering 1.5 with autoboxing and all) If you want a special "null" type of value, pick something like min_int, or -999 or some other invalid value.

    So, where are you at now? Still having these problems?
     
  9. nietsni3

    nietsni3 Guest

    thanks
    i ahve submitted the program to the prof already.
    i ended up doing it liek this :

    (basicly i just leave the tail at the end of the array without cutting it off)
     
  10. nietsni3

    nietsni3 Guest

    PHP:
    import java.io.*;
     
    import java.util.*;
     public class 
    Lab4
     
    {
         static 
    BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
         public static 
    void main (String[] argsthrows IOExceptionFileNotFoundException
         
    {
     
     
             
    int[] list1 = {0,2,5,6,3,2,5,4,0,2,0,0,00,2,0,0,4,5,0,02,56,2};
     
             
    removeAll(list1list1.length0);
     
     
             
    int[] list2 = new int[10];
     
             
    removeAll(list2list2.length4);
     
     
             
    int[] list3 = {1,1,1,1,1,1,1,1,1,1,1,1,1};
     
             
    removeAll(list3list3.length2);
     
     
     
         }
         public static 
    void removeAll(int[] inputArrayint lengthint removeItemthrows IOException
         
    {
             if (
    length <= 0System.out.println("Invalid length of array");
             else
             {
                 
    //print the inputArray
                 
    System.out.println("\nThe original array: " );
                 for (
    int m 0lengthm++)
                 {
                     
    System.out.print(inputArray[m] + " ");
                 }
                 
    int j=0;
                 
    int count 0;
     
                 
    //check to see if the array is empty:
                 
    while (inputArray[o] == 0)
                 {
                     
    o++;
                     if (
    == length -1) {System.out.println("\nThe array is empty."); return;}
                 }
     
     
                 
    System.out.println("\nSearch and remove all items: " removeItem);
     
     
                 
    //Start searching and removing:
                 
    for (int i 0length++)
                 {
     
     
     
     
                     
    //search for removeItem in inputArray
                     
    if (inputArray[i] == removeItem)
                     {
                         
    //add 1 into count everytime a removeItem is found
                         
    count count +1;
     
                         
    //from the location of that item, move  the rest of the items 1 index forward (stop at index length - 1 to avoid "Index out of bound")
                         
    for (length 1j++)
                         {
                             
    inputArray[j] = inputArray[j+1];
                         }
     
     
                         
    //subtract 1 from i so that after the for loop update i++, the new item that just moved into inputArray[j] will not be skipped
                         
    i--;
                     }
                 }
                 if (
    count== 0System.out.println("There is no item " removeItem " in the array");
                 else
                 {
                     
    System.out.println("The remain array after " removeItem "'s are removed is: ");
                     for (
    int k 0< (length count); k++)
                     {
                         
    System.out.print(inputArray[k] + " ");
                     }
                     
    System.out.println();
     
     
                 }
             }
         }
     }
     
     
     
     
    /*
     Output:
     
     The original array:
     0 2 5 6 3 2 5 4 0 2 0 0 0 2 0 0 4 5 0 2 56 2
     Search and remove all items: 0
     The remain array after 0's are removed is:
     2 5 6 3 2 5 4 2 2 4 5 2 56 2
     
     The original array:
     0 0 0 0 0 0 0 0 0 0
     The array is empty.
     
     The original array:
     1 1 1 1 1 1 1 1 1 1 1 1 1
     Search and remove all items: 2
     There is no 2 in the array
     Press any key to continue . . .
     */
     
     
     
  11. SL1200MK4

    SL1200MK4 New Member

    Joined:
    Sep 27, 2003
    Messages:
    1,552
    Likes Received:
    0
    Why would the removeAll need the 2nd argument [the length of the array], you can simply derive that infomation from the given inputArray, namely inputArray.length?
     

Share This Page