Showing posts with label Java Arrays. Show all posts
Showing posts with label Java Arrays. Show all posts

Thursday, 18 May 2023

Java array to arraylist

There are several ways to convert an array to an ArrayList in Java. Here are three of the most common methods:

Using Arrays.asList()

The Arrays.asList() method is the simplest way to convert an array to an ArrayList. It takes an array as its only argument and returns a List object that contains the elements of the array.

For example, the following code converts an array of strings to an ArrayList of strings:

Code snippet
String[] array = { "Hello", "World" };
List<String> list = Arrays.asList(array);

The list object returned by Arrays.asList() is backed by the original array. This means that if you modify the original array, the changes will be reflected in the list object.

Using Collections.addAll()

The Collections.addAll() method is another way to convert an array to an ArrayList. It takes two arguments: a List object and an array. The method adds all of the elements of the array to the List object.

For example, the following code converts an array of strings to an ArrayList of strings:

Code snippet
String[] array = { "Hello", "World" };
List<String> list = new ArrayList<>();
Collections.addAll(list, array);

The advantage of using Collections.addAll() over Arrays.asList() is that you can create a new ArrayList object and then add the elements of the array to it. This gives you more control over the type of List object that is created.

Using an iteration loop

The third way to convert an array to an ArrayList is to use an iteration loop. This method is the most flexible, but it is also the most time-consuming.

To convert an array to an ArrayList using an iteration loop, you first need to create a new ArrayList object. Then, you need to iterate through the array and add each element to the ArrayList object.

For example, the following code converts an array of strings to an ArrayList of strings:

Code snippet
String[] array = { "Hello", "World" };
List<String> list = new ArrayList<>();
for (String element : array) {
  list.add(element);
}

This method is the most flexible because you can use it to convert any type of array to any type of List object. However, it is also the most time-consuming because you need to iterate through the entire array.

Saturday, 3 January 2015

Java Arrays

An array in Java is an ordered collection of the similar type of variables. Java allows creating arrays of any dimensions. An array in Java is a bit different from C/C++. You can not specify the size of the array at the time of declaration. The memory allocation is always dynamic. Every array in Java is treated as an object with one special attribute length, which specifies the number of elements in the array.

One Dimensional array

Declaration

A one dimensional array is declared as:

type variable_name[];

The type specifies the base type of array elements, which specifies what type of elements can be stored in the array.

For example, the following array can be used to store int elements:

int marks[]; or int[] marks;

Declaration just specifies that array marks can be used to store integers but no memory allocation takes place at point of declaration.

Memory Allocation

Memory allocation for a Java array is done using operator new. The general form of the operator new is as follows:

var = new type[size];

The variable var is the name of an array, declared using the syntax as mentioned above. The type refers to the type of the elements that can be stored in the array. Size refers to the number of elements for which the memory is to be allocated dynamically.

For example, following array can store 20 elements:

marks = new int[20];

Thus obtaining an array in Java is a two step process:

1. declaration
2. memory allocation

A third step may be initializing the array elements.

The above two steps can be combined into one as follow:

int marks[] = new int[20];

Initialization

Third step is to initialize the array elements. For example the array allocated above can be initialized as:

for(int i=0; i<20; i++){
    marks[i] = 0;
}

The initialization with zero is not must as all the arrays of numeric types are initialized with 0.

Note: Array declaration, memory allocation and initialization steps can be combined into one as shown below:

int marks[] = {10, 2, 17, 77, 99, 90, 22, 34};

Array Length

In Java, all arrays store the allocated size in a variable named length. We can access the length of the array marks using the attribute length.

int len = marks.length;

Example: The following example demonstrate the use of attribute length and also demonstrates how to access the command line arguments.

class cmdLineDemo{
    public static void main(String args[]){
        int len = args.length;
        for(int i=0;i<len;i++){
            System.out.println(args[i]);
        }
    }
}

The JVM calls the main() and passes the command line arguments to it as an array of Strings. The length of the array (i.e. the number of the command line arguments) is obtained using attribute length in the above example. the for loop displays the command line arguments on the console/monitor.

Two dimensional array

Declaration

A two dimensional array is declared as:

type variable_name[][];

The type specifies the base type of array elements as in the case of one-dimensional array. For example, The following array can be used to store int elements:

int marks[][]; or int[][] marks;

Declaration just specifies that array marks can be used to store integers but no memory allocation takes place at point of declaration.

Memory Allocation

The general form of the operator new is as follows:

var = new type[rows][columns];

The variable var is the name of two dimensional array. the type refers to the type of the elements that can be stored in the array. The first index indicates the number of rows and the second index indicates the number of columns in the two dimensional array to be allocated dynamically.

For example, following array can store a table consisting of 5 rows and 4 elements:

marks = new int[5][4];

The above two steps can be combined into one as follows:

int marks[][] = new int[5][4];