Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Program-27

Q. Develop a Java source code to remove an object from the list variable.
Source Code: import java.util.List; import java.util.LinkedList; class A1{ String name; public void addName(String name){ this.name=name; } public void show(){ System.out.println("The string is "+name); } } class StoreObject1{ public static void main(String[] args){ A obj=new A(); A obj1=new A(); A obj2=new A(); Object demoobj; obj.addName("XYZ"); obj1.addName("PQR"); obj2.addName("DEF"); //creating object of list List list=new LinkedList();

//storing class object in list variable list.add(obj); list.add(obj1); list.add(obj2); //iterating the list object for(int i=0;i<list.size();i++){ demoobj=list.get(i); A obj4=(A) demoobj; obj4.show(); } //removing the object from the list variable list.remove(obj1); for(int i=0;i<list.size();i++){ demoobj=list.get(i); A obj4=(A) demoobj; obj4.show(); } } }

Output: The string is XYZ The string is PQR The string is DEF The string is XYZ The string is DEF

You might also like