Skip to main content

Post 2: Core Java :Converting Array to List in java


Converting Array to List in java

Array class has a asList() method that helps to convert array to List. This method copies an array into List. When we use asList() method, the array and list both shares the same heap space. When update is made to the list, it reflects the changes to array and vice-versa.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Arrays;
import java.util.List;
 
public class ArrayToList {
 
 public static void main(String[] args) {
  String[] names = { "Neel", "Clay", "Adams", "Joseph", "Jack" };
 
  // Converting Array to List 
  List aList = Arrays.asList(names);
 
  System.out.println("Size of list=" + aList.size());
  System.out.println("aList Values=" + aList);
 
  //changing the aList values
  aList.set(2, "Dinesh");
 
  // printing aList values
  for (String name : names) {
   System.out.println(name);
  }
 }
}
If you look at the output below you will notice that, after changing the values in list, the changes reflected in the array.
Output
Converting Array to List in java

Comments

Popular posts from this blog

Index: JSF

Java Server Faces Basics Post 1: JSF : Introduction to JSF  Post 2: JSF:  configuration files  Post 3: JSF: Installation Post 4: JSF: Your first JSF project   Standard JSF Tags Post 1: JSF: textbox example Post 2: JSF: password example Post 3: JSF: textarea example JSF 2 GraphicImage Example Post 4: JSF: GraphicImage Example Post 101 : JSF : The valueChangeListener Attribute Example

Index: iReport

iReport for Java ------------------------------------------------------------ Titles and Headers ------------------------------------------------------------ Post 1: iReport : Downloading, installing, and running JasperReports and iReport Post 2: iReport : Creating your first "Hello World" report   Post 3: iReport : Understanding iReport Elements Post 4: iReport : Creating and sizing the title for your report Post 5: iReport: Inserting a company logo in the title of your report  Post 6: iReport : Installation of PostgreSQL Post 7: iReport : Creating a report from relational data Post 8: iReport : Using dynamic titles that can change during report processing. Post 9: iReport : Adding a simple header to your report Post 10: iReport : Setting margins for your report and aligning the report header relative to report margins  ------------------------------------------------------------ Body and Footer ---------------------------------------------------------...