“Liste in Java” Code-Antworten

So erstellen Sie die Java -Liste

//Creating arraylist example 
ArrayList<String> list = new ArrayList<String>();  
 
//Adding objects in arraylist 
list.add("Mango");    
list.add("Banana");   

//Change the element (index,"new value")
list.set(1,"Dates"); 

//Return the 2nd element, because index starts from 0
System.out.println("Returning element: " + list.get(1)); 
EeroVäkiparta

Java Wie kann ich Liste verwenden?

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

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

Liste von Java

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

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

var list3 = new ArrayList<String>();
Aryqs Ipsum

Liste in Java

ArrayList<String> values = new ArrayList<String>();
        values.add("apple");
        values.add("boy");
        values.add("cat");
        values.add("dog");
        values.add("elephant");
        //apple boy cat dog elephant
        values.add(1,"xyz");
        //apple xyz boy cat dog elephant
codelearner

Java -Liste

// 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);
    }
}
MrMike

Liste der Liste 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);
    }
}
Pankaj Kumar Prasad

Ähnliche Antworten wie “Liste in Java”

Fragen ähnlich wie “Liste in Java”

Weitere verwandte Antworten zu “Liste in Java” auf Java

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen