JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

Conversion between Array and List types

August 27, 2007 by Krishna Srinivasan Leave a Comment

In some cases, we may need to convert an array to a list or vice versa. The method asList() is available in the Arrays class, and the toArray() method in list and set classes serve this purpose.

also read:

  • Java Tutorials
  • Java EE Tutorials
  • Design Patterns Tutorials
  • Java File IO Tutorials

The Arrays.asList() method converts an array into a list and the size of the list is fixed. Let us see a sample of how the asList() method works,

[code lang=”java”]
Integer[] numArr = {100, 200, 300, 400, 500};
List<integer> numList = Arrays.asList(numArr);
System.out.println("size of list is : " + numList.size());
System.out.println("elements in list : " + numList);
numList.set(3,600); // Update the 4th element in list
System.out.println("After update, elements in list : " + numList);
System.out.println("4th element in list is : " + numList.get(3));
System.out.println("4th element in array is : " + numArr[3]);
[/code]

The output of the above code is,
[code]
size of list is : 5
elements in list : [100, 200, 300, 400, 500]
After update, elements in list : [100, 200, 300, 600, 500]
4th element in list is : 600
4th element in array is : 600
[/code]

In the above example, we saw that the array numArr is converted to a list numList which is of the same size as the array. We also note that any further changes in the list numList have updated the array numArr also.

The toArray() method converts a set or list to an array. This method has two forms of invocation. One form of the toArray() method returns a Object array, and another form returns the array of the type that is passed as the argument. Let us see the following code to know how the toArray() method works,

[code lang=”java”]
Set<string> langSet = new HashSet<string>();
langSet.add("C");
langSet.add("C++");
langSet.add("Java");
langSet.add("Perl");
langSet.add("Sql");
System.out.println("size of the set : " + langSet.size());
Object[] langArr = langSet.toArray(); // this returns an object array
System.out.println("length of array langArr : " + langArr.length);
String[] arr2 = new String[5];
arr2 = langSet.toArray(arr2); // this returns an array of same type as arr2 i.e a String array
System.out.println("length of array arr2 : " + arr2.length);
System.out.print("elements in array arr2 : ");
for(String str : arr2)
System.out.print(str + " ");
[/code]

The output of the above code is,
[code]
size of the set : 5
length of array langArr : 5
length of array arr2 : 5
elements in array arr2 : Java C++ C Perl Sql
[/code]

Filed Under: Java Tagged With: Core Java

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved