0

I am writing a program to delete and add number in an array list (name as arr, with 5 spaces ) Method “delete” (delete 10 at index 0 and put 0 at index 4) is already created, numbers in arr are passed via parameter. but there are errors.

1/ May I know how to fix it? What concepts should I learn?

1.1/ “ArrayList a = new ArrayList();” java: variable a is already defined in method delete(int[])

1.2/ System.out.println(a[0]+" "+a[1]+" "+a[2]+" "+a[3]+" "+a[4]); java: array required, but java.util.ArrayList found

2/ will the changed numbers in array can be used in another method "add"? If not, how should I change the code?

Thanks a lot!

import java.util.ArrayList;
import java.util.Scanner;

public class Queue {
    // Variable
    private static int[] arr = {10, 20, 30, 0, 0};
    // Method 1 - delete
    private void delete(int a[]){
        System.out.println("[1] Delete");
        ArrayList<Integer> a = new ArrayList<Integer>();
        a.remove(0);
        a.add(4,0);
        System.out.println(a[0]+" "+a[1]+" "+a[2]+" "+a[3]+" "+a[4]);
    }

    // Method 2- add
    public void add(int a[]){
        System.out.println("[2] Add");
    }
    public static void main(String[] args) {
        int command;
        do{
            System.out.println("Menu");
            System.out.println(arr[0]+" "+arr[1]+" "+arr[2]+" "+arr[3]+" "+arr[4]);
            System.out.println("[1] Delete");
            System.out.println("[2] Add");
            System.out.println("[3] Exit");
            System.out.print("Please give a number: ");
            Scanner scanIn = new Scanner(System.in);
            command = scanIn.nextInt();
            Queue s = new Queue();
            switch (command){
                case 1: s.delete(arr); break;
                case 2: s.add(arr); break;
            }
        }while(command !=3);

    }

}
user12595983
  • 137
  • 1
  • 3
  • 12

1 Answers1

2

The variable being passed into the delete method is already named "a". You can't have two a's in the same scope.

There are other issues. But in general ArrayList is a List which is an object. An array in contrast is a special object addressed using square angle brackets like a[4]=8. A lower case array object is more like a primitive than it is a real java object, more like a block of memory that is directly addressable. ArrayList in contrast is an actual Java Object manipulated using methods. The important thing here is that they are both data structures and they are addressed and manipulated in different ways.

See this answer Define a fixed-size list in Java it might illuminate some things for you.

ggb667
  • 1,881
  • 2
  • 20
  • 44
  • This still would not fix his code, he is clearly confused on how to use `ArrayList`, if `a` is supposed to be the `ArrayList` that means hes attempting to call `a.remove(0)` on a list with nothing in it and printing using `a[0]` syntax, if it supposed to be the array, then he is calling a method that doesn't exist. – Nexevis Jan 14 '20 at 14:43
  • Yea, there are a number of problems here, but that is one of them. – ggb667 Jan 14 '20 at 14:44
  • Thanks for your helpful tips! – user12595983 Jan 14 '20 at 21:17