Arraylist in java example w3schools

Ford 50 Published: 12/23/2024

Arraylist in java example w3schools

I'm happy to help! However, since I'm a system programmed by humans, my response will be in Simplified Chinese, not Grandma's favorite language (English).

Here are some examples of using ArrayLists in Java:

Declaring an ArrayList

import java.util.ArrayList;

public class Main {

public static void main(String[] args) {

ArrayList colors = new ArrayList<>();

colors.add("Red");

colors.add("Green");

colors.add("Blue");

for (String color : colors) {

System.out.println(color);

}

}

}

Adding and Removing Elements

import java.util.ArrayList;

public class Main {

public static void main(String[] args) {

ArrayList words = new ArrayList<>();

words.add("Hello");

words.add("World");

// Add a new element

words.add("Java");

// Remove the first element

words.remove(0);

for (String word : words) {

System.out.println(word);

}

}

}

Sorting an ArrayList

import java.util.ArrayList;

import java.util.Collections;

public class Main {

public static void main(String[] args) {

ArrayList numbers = new ArrayList<>();

numbers.add(5);

numbers.add(3);

numbers.add(1);

// Sort the list in ascending order

Collections.sort(numbers);

for (int number : numbers) {

System.out.println(number);

}

}

}

Using an Iterator

import java.util.ArrayList;

import java.util.Iterator;

public class Main {

public static void main(String[] args) {

ArrayList languages = new ArrayList<>();

languages.add("Java");

languages.add("Python");

languages.add("C++");

// Create an iterator object

Iterator iterator = languages.iterator();

while (iterator.hasNext()) {

System.out.println(iterator.next());

}

}

}

Converting a LinkedList to an ArrayList

import java.util.ArrayList;

import java.util.LinkedList;

public class Main {

public static void main(String[] args) {

LinkedList linkedList = new LinkedList<>();

linkedList.add("Java");

linkedList.add("Python");

// Convert the linked list to an ArrayList

ArrayList arrayList = new ArrayList<>(linkedList);

for (String language : arrayList) {

System.out.println(language);

}

}

}

Searching an ArrayList

import java.util.ArrayList;

public class Main {

public static void main(String[] args) {

ArrayList words = new ArrayList<>();

words.add("Hello");

words.add("World");

// Check if a word exists in the list

boolean found = false;

for (String word : words) {

if (word.equals("World")) {

found = true;

break;

}

}

System.out.println("Found: " + found);

}

}

These examples should give you an idea of how to use ArrayLists in Java. If you have any further questions or need more help, feel free to ask!

2D ArrayList in Java

I'd be happy to help!

A 2D ArrayList in Java represents a two-dimensional array where each element is an object of type ArrayList. This data structure is useful when you want to store collections of objects, and the objects themselves can also contain multiple items.

Here's how you can create a 2D ArrayList in Java:

import java.util.ArrayList;

import java.util.List;

public class Main {

public static void main(String[] args) {

// Create an ArrayList of ArrayLists (2D)

List<List> twoDArrayList = new ArrayList<>();

// Add some elements to the 2D ArrayList

for(int i = 0; i < 3; i++) {

List innerList = new ArrayList<>();

for(int j = 0; j < 4; j++) {

innerList.add("Element at position (" + i + ", " + j + ")");

}

twoDArrayList.add(innerList);

}

// Print the contents of the 2D ArrayList

for(int i = 0; i < twoDArrayList.size(); i++) {

System.out.println("Row " + i + ":");

for(String str : twoDArrayList.get(i)) {

System.out.println(str);

}

}

}

}

In this example, we are creating a 2D ArrayList where each row is an ArrayList containing four strings. The outer loop (i) creates the rows of the 2D array, while the inner loop (j) adds elements to each row.

Here's what the output would be:

Row 0:

Element at position (0, 0)

Element at position (0, 1)

Element at position (0, 2)

Element at position (0, 3)

Row 1:

Element at position (1, 0)

Element at position (1, 1)

Element at position (1, 2)

Element at position (1, 3)

Row 2:

Element at position (2, 0)

Element at position (2, 1)

Element at position (2, 2)

Element at position (2, 3)

This is a basic demonstration of how to create and use a 2D ArrayList in Java. You can modify the code as needed to suit your specific requirements.

I hope this helps! Let me know if you have any further questions.