Browsed by
Tag: arraylist

Array vs Arraylist java

Array vs Arraylist java

Array vs ArrayList java: Array: 1. Size must be defined during initialization itself. 2. Size can not be resized once initialized. int[] myArraySize = new int[10]; //Possible int[] myArray = new int[];  // Not possible 3. Size of the array calculated using length. int[] myArr = new int[9];System.out.println("size of the array : "+myArr.length); //prints 9 4. Both objects and primitives can be stored. int[] myArr = new int[9]; //Possible (primitive data type) Integer[] myArrInteger = new Integer[10]; //Possible (wrapper class…

Read More Read More

1,081 total views, 1 views today

Array of ArrayList in Java example

Array of ArrayList in Java example

Array of ArrayList: Array of ArrayList has to be typecasted first. ArrayList array has to be initialized first with ArrayList. Then we have to add the values to the ArrayList. Iterator has to be created separately for each arraylist of array. Program: import java.util.ArrayList; import java.util.Iterator; public class ArrayofArrayList { public static void main(String[] args) { ArrayList[] myArrayArrayList = (ArrayList[])new ArrayList[10]; // 10 is the size of the array not arraylist myArrayArrayList[4] = new ArrayList(); //The array has to be…

Read More Read More

874 total views, 1 views today

List Inside List Iteration in Java

List Inside List Iteration in Java

List: Usually we can iterate a list using iterator or foreach loop. Here we are first iterating the mainlist which holds sublists. And the sublists are iterated and printed the values. Program: package com.ngdeveloper.com; import java.util.ArrayList; import java.util.List; public class ListInsideListIteration { public static void main(String[] args) { List mainList = new ArrayList(); List mobileCompanies = new ArrayList(); List laptopCompanies = new ArrayList(); mobileCompanies.add(“Nokia”); mobileCompanies.add(“Samsung”); mobileCompanies.add(“Apple”); laptopCompanies.add(“Dell”); laptopCompanies.add(“Lenovo”); laptopCompanies.add(“Hp”); mainList.add(mobileCompanies); mainList.add(laptopCompanies); for (Object subList : mainList) { for (Object listValues…

Read More Read More

764 total views, no views today

Arraylist iteration in java

Arraylist iteration in java

Arraylist: Arraylist is used to hold any number of elements. Program: package com.ngdeveloper.com; import java.util.ArrayList; import java.util.Iterator; public class ArrayListIteration { public static void main(String[] args) { // Arraylist which holds only string. ArrayList<String> al = new ArrayList<String>(); //Adding below companies names to arraylist. al.add("Hp"); al.add("Samsung"); al.add("Dell"); al.add("Apple"); al.add("Compaq"); //Iterator Iterator iter = al.iterator(); //Iterating arraylist using while loop. while(iter.hasNext()){ System.out.println("ArrayList Iteration using while loop "+iter.next()); } //Iterating arraylist using foreach for (String string : al) { System.out.println("ArrayList Iteration using…

Read More Read More

1,193 total views, 1 views today

Difference between ArrayList and Collection

Difference between ArrayList and Collection

Collection: Collection col = new ArrayList(); Iterator col_iter = col.iterator(); You will not able to get the arraylist value by its index. (col_iter.get(index) method does not exist). Set firstSet = new HashSet(); You can able to assign the col_iter = firstSet; without any casting. ArrayList: ArrayList al = new ArrayList(); Iterator al_iter = al.iterator(); You can access the arraylist value by its index. (al_iter.get(index) method helps to get the value by its index). Set secondSet = new HashSet(); You must…

Read More Read More

No views yet