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);
}
}