Hi all, I am currently trying to pass an arraylist as a parameter for adding and deleting a student from the arraylist.
Well, I have just learned about arraylist. Is it possible to pass an arraylist as parameter.
Your help will be highly appreciated. Thanks.

My codes below:

package student;
import java.io.*;
import java.util.*;

public class StudentProtocol {

	private String id;
	private String name;
	private String module_name;
	private String marks;
	BufferedReader userInput=new BufferedReader(new InputStreamReader(System.in));
	ArrayList<String> myArr =new ArrayList<String>();
	
	//constructor	
	public StudentProtocol(String id,String name,String module_name,String marks){
		this.id=id;
		this.name=name;
		this.module_name=module_name;
		this.marks=marks;
	}
	
	//
	public String getId(){
		return id;
	}
	
	public String getName(){
		return name;
	}
	
	public String getModuleName(){
		return module_name;
	}
	
	public String getMarks(){
		return marks;
	}
	
	//mutator
	
	public void setId(String id){
		this.id=id;
	}
	
	public void setName(String name){
		this.name=name;
	}
	
	public void setModuleName(String module_name){
		this.module_name=module_name;
	}
	
	public void setMarks(String marks){
		this.marks=marks;
	}
	
	public void displayStudentDetails(){
		System.out.println("ID is "+id);
		System.out.println("Name is "+name);
		System.out.println("Module name is "+module_name);
		System.out.println("Marks is "+marks);
	}
	
	public void addDetails()throws IOException{
		BufferedReader userInput=new BufferedReader(new InputStreamReader(System.in));
		ArrayList<String> myArr =new ArrayList<String>();
		myArr.add(id);
		myArr.add(name);
		myArr.add(module_name);
		myArr.add(marks);
		
		System.out.println("Adding a new student");
		System.out.println("Enter id:");
		String id = userInput.readLine();
		System.out.println("Enter name:");
		String name = userInput.readLine();
		System.out.println("Enter module name:");
		String module_name = userInput.readLine();
		System.out.println("Enter marks:");
		String marks = userInput.readLine();		
		System.out.println("ID is "+id);
		System.out.println("Name is "+name);
		System.out.println("Module name is "+module_name);
		System.out.println("Marks is "+marks);
		
		
	}
	
	public  void removeDetails(ArrayList<String> myArr) throws IOException{
		displayStudentDetails();
		//BufferedReader userInput = new BufferedReader (new InputStreamReader (System.in));
		
		System.out.println("Please,enter a student id to delete:");
		String id =userInput.readLine();
		System.out.println("Size of array before removing item:"+myArr.size());
		myArr.remove(myArr);
		System.out.println(id+"has been removed!");
		System.out.println("Size of array after removing item:"+myArr.size());
		
	}
	/*public void modifyDetails() throws IOException{
		
		ArrayList<String> a= myArr;
		System.out.println(myArr);
		
		
				
		
	}*/
	
	
	public static void main(String[] args, ArrayList<String> id) throws IOException {
		StudentProtocol s1= new StudentProtocol("89023","Rita","cse 3000Y","32.5");
		s1.addDetails();
	}

}

Yes, it's completely possible. Just like passing anything else.

Beware: you declare the list on line 12, then on line 66 you declare another one with exactly the same name. Probably a mistake.

Yes, it's completely possible. Just like passing anything else.

Beware: you declare the list on line 12, then on line 66 you declare another one with exactly the same name. Probably a mistake.

Thank you for your reply.
Yes, I was trying to figure out how to pass the arraylist as a parameter in the addDetails() method. Previously, I have tried to put it as such:

[public void addDetails(ArrayList<String> myArr) throws IOException]

but I was not having the results I expected.

Thank you very much for your help.

Yes, you can pass it like that, but because you have declared it as an instance variable on line 12 you can use it any (non-static) method in the same class without passing it explicitly.

This article has been dead for over six months. Start a new discussion instead.