skip to Main Content

I made this code with some help, and am new to Java. I was wondering if I can remove and add something to an array. I tried to add a digit to the end of a array but i’m trying to remove it. To make it a challenge I don’t want to use the old array variable. I didn’t import the java.util for the add{} statement and it worked fine, but when I add it for the delete method the code stopped entirely. I spent a few days trying to figure this out, by looking online on multiple sites to see what I can do as well as asked a couple of schoolmates how (because they learned Java), but they couldn’t figure it out either. I’m learning Java on the side in elementary school so I can be a Game maker so I want to finish this before summer ends. How do I make it so I can print the deleted array after the added one. I learned about array lists, but I saw online about Static Methods. How do I do it with that? I put the code on the bottom. This one doesn’t have the import so its half working.

public class Arrays {

  public static int[] add(int[] arr, int val) {
    int[] newArray = java.util.Arrays.copyOf(arr, arr.length + 1);
    newArray[arr.length] = val;
    return newArray;
}
public static int[] del() {
 int[] del_arrays = {1, 2, 3};
 int[] DelArray = java.util.Arrays.removeElement(del_arrays [-1] );
 return DelArray;
}


public static void main(String[] args) {
    int[] array = { 1, 2, 3 };
    System.out.println(java.util.Arrays.toString(array));
    int[] new_array = add(array, 4);
    System.out.println(java.util.Arrays.toString(new_array));
    del();
   
}
 

} 

Terminal

/usr/bin/env /Library/Java/JavaVirtualMachines/jdk-18.0.1.1.jdk/Contents/Home/
bin/java -agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:52122 --enable-preview -XX:+Sho
wCodeDetailsInExceptionMessages -cp /private/var/folders/hx/s_5smg5s6510w9_szj6xk8200000gn/T/vscodesws_37f78/jdt
_ws/jdt.ls-java-project/bin Arrays 
[1, 2, 3]
[1, 2, 3, 4]
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
        The method removeElement(int) is undefined for the type Arrays

        at Arrays.del(Arrays.java:11)
        at Arrays.main(Arrays.java:21)
aadavnnimalthas@Aadavns-iMac ~ % 

Problems Section

Arrays.java 
Arrays.java is a non project file, only syntax errors are reported

I am using Visual Studio on a iMac

2

Answers


  1. Just like your add method, you need to provide an array and element index to delete. For a static method you need to provide everything it needs. In this case, you need to provide the array and the index of the element you intend to delete from that array. Remember the edge case of what happens when you want to delete the last element of an array or an array with no elements.

    Something like the following should suffice:

    public static int[] del(int[] arrayToDeleteFrom, int indexToDelete)
    {
        boolean empty = false;
        
        if(arrayToDeleteFrom.length <= 1)
            empty = true;
            
        int[] newArrayAfterDelete = new int[(empty ? 0 : arrayToDeleteFrom.length - 1)];
        
        for(int index = 0; index < arrayToDeleteFrom.length; index++)
        {
            //if the index is the one we want to delete, then skip adding it to our new array
            if(indexToDelete == index)
                continue;
            else
                newArrayAfterDelete[index] = arrayToDeleteFrom[index];
        }
     
        return newArrayAfterDelete;
    }
    

    In order to call this method you need to provide all the required parameters. For this del method you need to first provide an int array and second, provide an index of the element you wish to delete.

      int[] a = { 1, 2, 3 };
      System.out.println(Arrays.toString(a));
      a = add(a, 4);
      System.out.println(Arrays.toString(a));
      a = del(a, 1); //<-- provide the array AND the index of the element to remove, with index 1 we should remove number 2 from the a array
      System.out.println(Arrays.toString(a));
    
    Login or Signup to reply.
  2. This is probably what you are trying to do. The add method append a new element to the array and the delete method remove the last element. Del would look like the other answer if you want to delete specific element.

    import java.util.Arrays;
    class Main {
      public static int[] add(int[] arr, int val) {
          int[] newArray = Arrays.copyOf(arr, arr.length + 1);
          newArray[arr.length] = val;
          return newArray;
       }
       public static int[] del(int[] arr) {
          return Arrays.copyOf(arr, arr.length - 1);
       }
       public static void main(String[] args) {
          int[] a = { 1, 2, 3 };
          System.out.println(Arrays.toString(a));
          a = add(a, 4);
          System.out.println(Arrays.toString(a));
          a = del(a);
          System.out.println(Arrays.toString(a));
       }
    }
    

    Output

    [1, 2, 3]
    [1, 2, 3, 4]
    [1, 2, 3]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search