0% found this document useful (0 votes)
55 views

Program-29: Q. Develop A Java Source Code To Demonstrate Conversion of Linked List To Array Using Toarray Method

The code sample demonstrates how to convert a linked list to an array using the toArray() method in Java. An ArrayList is created and three objects of class A are added to it with different string names. The toArray() method is then used to convert the ArrayList to an Object array. A for loop iterates through the array and casts each element to type A to call the show() method and display the stored string names.

Uploaded by

Catherine Nipps
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Program-29: Q. Develop A Java Source Code To Demonstrate Conversion of Linked List To Array Using Toarray Method

The code sample demonstrates how to convert a linked list to an array using the toArray() method in Java. An ArrayList is created and three objects of class A are added to it with different string names. The toArray() method is then used to convert the ArrayList to an Object array. A for loop iterates through the array and casts each element to type A to call the show() method and display the stored string names.

Uploaded by

Catherine Nipps
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Program-29

Q. Develop a Java source code to demonstrate conversion of Linked List to Array using toArray() method.
Source Code: import java.util.List; import java.util.ArrayList; class A3{ String name; public void addName(String name){ this.name=name; } public void show(){ System.out.println("The string is "+name); } } class StoreObject3{ public static void main(String[] args)throws IO Exception{ A obj=new A(); A obj1=new A(); A obj2=new A(); Object[] arrayOfA=new Object[3]; Object[] arrayOfA1=new Object[3]; Object demoobj,demoobj1; obj.addName("XYZ"); obj1.addName("PQR"); obj2.addName("DEF"); ArrayList list=new ArrayList(); list.add(obj);

list.add(obj1); list.add(obj2); arrayOfA1=list.toArray(); for(int i=0;i<list.size();i++){ demoobj=list.get(i); A obj4=(A) arrayOfA[i]; obj4.show(); } } }

You might also like