0

I need to implement a delete method WITHOUT USING AN ARRAY LIST. I need to use a set of loops to do it. Here is my delete method and add method as well as any other important variables used. Any advice on what is wrong with my code would be great.

EDITED: Changed the comparing of references to values. Seems to work repeatedly. final int MAX_DEVICES = 5;

   // Array of devices
   private Device list[] = new Device[MAX_DEVICES];

   // Number of Devices currently in the list
   // "Valid" Devices are stored in cells 0 - (numDevices - 1)
   private int numDevices = 0;

   Scanner stdin;  // read from stdin

 private void Add()
   {
      String thisName;
      int numThisRead;
      float thisInitVal;

      thisName = stdin.next();
      numThisRead = stdin.nextInt();
      thisInitVal = stdin.nextFloat();

      if(numDevices > MAX_DEVICES)
         System.out.println("The List was full. " + thisName +
               " was not added to the list.");
      else
      {
         Device myDevice = new Device(thisName, numThisRead, thisInitVal);
         list[numDevices] = myDevice;
         numDevices ++;
         System.out.println(thisName + " device has been added to the list.");
      }
   }

   private void Delete() //ASK QUESTION
   {
      String thisDelete;
      thisDelete = stdin.next();
      for(int i = 0; i < MAX_DEVICES; ++i)
      {
         if(list[i].getName().equals(thisDelete)) //if you find the name
         {
            System.out.println(list[i].getName() + " was deleted from the "
                  + "list.");
            for(int j = i; j < numDevices - 1; j++)
               list[j] = list[j + 1];
            numDevices--;
            return;
         }
      }
      System.out.println(thisDelete + " not deleted. It is not in the list.");
   }
3
  • 1
    CLOSED. A more in depth debug I found my issue. Thanks to those who looked at this. Commented Feb 4, 2014 at 22:31
  • When you say if(list[i].getName() == thisDelete), is this what you really want? Here you are comparing references and not values. Commented Feb 4, 2014 at 22:33
  • That was the issue I found. I'll fix it. Commented Feb 4, 2014 at 22:34

1 Answer 1

1

If you need to avoid using data type List, you can place the objects in the array. Then you can declare an array one element smaller than the current array and copy all the elements, except for the one you want deleted, over into the new array. Then return the new array.

Sign up to request clarification or add additional context in comments.

4 Comments

Just for future reference: How would I copy over all but one. I do know how to copy an array in several ways, but how would I leave out 1 option?
for element j, copy all the elements between i and j-1, and j+1 and array.length-1 into the new array.
Would my delete method, officially delete the object and move the other objects back to fill in the empty space then? I can't add any object data whatsoever, but I'm just drawing a blank. If I were to need to do that, would I just adjust my for loop to for(int j =0; j < i-1 && i+1 < numDevices - 1; j++) work?
The for loop you mentioned would break when j==i-1. The delete method you wrote would duplicate list element j+1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.