Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

list in java

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
public class Main {
   public static void main(String[] args) {
      List<String> list = new ArrayList<String>();
      list.add("a");
      list.add("b");
      list.add("c");
      list.add("d");
      list.add("e");
      list.add("f");
      
      //On met la liste dans le désordre
      Collections.shuffle(list);
      System.out.println(list);
      
      //On la remet dans l'ordre
      Collections.sort(list);
      System.out.println(list);
      
      Collections.rotate(list, -1);
      System.out.println(list);
      
      //On récupère une sous-liste
      List<String> sub = list.subList(2, 5);
      System.out.println(sub);
      Collections.reverse(sub);
      System.out.println(sub);
      
      //On récupère un ListIterator
      ListIterator<String> it = list.listIterator();
      while(it.hasNext()){
         String str = it.next();
         if(str.equals("d"))
            it.set("z");
      }
      while(it.hasPrevious())
         System.out.print(it.previous());
      
   }
}
Comment

list in java

public static void main(String[] args)
    {
        List<Integer> l1 = new ArrayList<Integer>();
  
        l1.add(1, 2);    // Custom inputs
    
        System.out.println(l1); // Print the elements inside the object
   
        l1.add(1);
        l1.add(2);
        l1.add(3);
  
        // Will add list l2(ASSUMING THAT l2 is ANOTHER LIST) from 1 index
        l1.addAll(1, l2);
        l1.remove(1);      
        System.out.println(l1.get(3)); // using get() method
        l1.set(0, 5); // Replace 0th element with 5
    }
Comment

list in java

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class testo {
    public static void main(String args[]){
    List<Object> list = new ArrayList<Object>();
    list.add("1");
    list.add("2");
    list.add("3");
    list.add("4");
    list.add("5");

    Iterator<Object> it = list.iterator();
    while(it.hasNext()){

        System.out.println(it.next());
    }
}
}
Comment

list of lists java


import java.util.ArrayList;
import java.util.List;
 
public class main {
    public static void main(String[] args){
        List<List<Integer>> lists = new ArrayList<List<Integer>>();
        List<Integer> num1 = Arrays.asList(new Integer[] {1,2,3,4});
        List<Integer> num2 = Arrays.asList(new Integer[] {7,9,3,4});
        lists.add(num1);
        lists.add(num2);
        System.out.println(lists);
    }
}
Comment

list of lists java

List<ArrayList<String>> listOfLists = new ArrayList<ArrayList<String>>();
Comment

list java

    import java.util.*;  
    public class ListExample1{  
    public static void main(String args[]){  
     //Creating a List  
     List<String> list=new ArrayList<String>();  
     //Adding elements in the List  
     list.add("Mango");  
     list.add("Apple");  
     list.add("Banana");  
     list.add("Grapes");  
     //Iterating the List element using for-each loop  
     for(String fruit:list)  
      System.out.println(fruit);  
      
    }  
    }  
Comment

java list

import java.util.*;
var list = new ArrayList<String>();
list.add("Hello World!");
Comment

Java List

// Java Program to illustrate the
// addition of elements in a List
 
import java.util.*;
public class GFG {
 
    public static void main(String args[])
    {
 
        // Creating a List
        List<String> al = new ArrayList<>();
 
        // Adding elements in the List
        al.add("mango");
        al.add("orange");
        al.add("Grapes");
 
        // Iterating the List
        // element using for-each loop
        for (String fruit : al)
            System.out.println(fruit);
    }
}
Comment

Java How to use List?

// ArrayList implementation of List
List<String> list1 = new ArrayList<>();

// LinkedList implementation of List
List<String> list2 = new LinkedList<>();
Comment

list of java

var list1 = List.of(1, 2);

var list2 = new ArrayList<>(List.of(3, 4));

var list3 = new ArrayList<String>();
Comment

list of list in java

// Java code to demonstrate the concept of
// list of lists using iterator
  
import java.util.*;
  
class List_of_list {
  
    // Iterate the 2D list using Iterator
    // and print each element
    public static <K> void
    iterateUsingIterator(List<List<K> > listOfLists)
    {
        // Iterator for the 2D list
        Iterator listOfListsIterator
            = listOfLists.iterator();
  
        System.out.println("[");
        while (listOfListsIterator.hasNext()) {
  
            // Type cast next() method
            // to convert from Object to List<K>
            List<K> list = new ArrayList<K>();
  
            list = (List<K>)listOfListsIterator.next();
  
            // Iterator for list
            Iterator eachListIterator
                = list.iterator();
  
            System.out.print("  [");
            while (eachListIterator.hasNext()) {
  
                System.out.print(
                    "  "
                    + eachListIterator.next()
                    + ", ");
            }
            System.out.println("], ");
        }
        System.out.println("]");
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        // List of Lists
        ArrayList<List<Integer> > listOfLists
            = new ArrayList<List<Integer> >();
  
        // Create N lists one by one
        // and append to the list of lists
        List<Integer> list1
            = new ArrayList<Integer>();
        list1.add(5);
        list1.add(10);
        listOfLists.add(list1);
  
        List<Integer> list2
            = new ArrayList<Integer>();
        list2.add(1);
        listOfLists.add(list2);
  
        List<Integer> list3
            = new ArrayList<Integer>();
        list3.add(20);
        list3.add(30);
        list3.add(40);
        listOfLists.add(list3);
  
        // Iterate the 2D list
        iterateUsingIterator(listOfLists);
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java super keyword 
Java :: remove duplicates from string in java 
Java :: How to get a context in a recycler view adapter 
Java :: Loop in singly linked list 
Java :: linkedlist java 
Java :: java compute sum and average of array elements 
Java :: how to find sum of the digit of the numbers in java 
Java :: declare generic set java 
Java :: how to concatenate two strings in java 
Java :: If you are using the git profile, you need to set a Git URI in your configuration. If you have set spring.cloud.config.server.bootstrap=true, you need to use a composite configuration. 
Java :: remove empty elements from array java 
Java :: how to get index of arraylist in java 
Java :: print anything in java 
Java :: loop array using stream java 
Java :: java check if string contains multiple words 
Java :: Java get integer color from rgb 
Java :: java creating a stack 
Java :: check if char is letter or digit 
Java :: how to replace in java 
Java :: java opp 
Java :: java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config 
Java :: java sort arraylist of floats 
Java :: spring yml property boolean 
Java :: java check if year is leap 
Java :: give ram to java 
Java :: Implementing the Vector Class in java list 
Java :: How to initialize a 3d array in Java? 
Java :: list of strings java 
Java :: Send keybord key P in selenium java 
Java :: random int between two values 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =