Java ArrayList Examples
- Java ArrayBlockingQueue Example
- Java Convert Array to Set
- Copying part of Array in Java Example
- Sorting an Array using Arrays.sort() Method
- Check Two Arrays are Deeply Equal
- Java Arrays.copyOf() Example - Copying an Array
- Java Arrays.binarySearch() Example - Searching an Array
- Java Arrays.asList() Example - Convert Array to ArrayList
- Java Convert Array to Stack Example
- What's the Simplest Way to Print a Java Array?
- Java Pyramid Examples
- How to Check if Two Arrays are Equal in Java
- Create and Initialize Two Dimensional Array in Java
- How to Initialize an Array in Java in One Line
- Java Array - Create, Declare, Initialize and Print Examples
- Java Array Simple Program
Array Methods with Examples
- Java ArrayList add() Method
- Java ArrayList addAll() Method
- Java ArrayList clear() Method
- Java ArrayList contains() Method
- Java ArrayList ensureCapacity() Method
- Java ArrayList clone() Method
- Java ArrayList get() Method
- Java ArrayList indexOf() Method
- Java ArrayList remove() Method
- Java ArrayList removeAll() Method
- Java ArrayList removeIf() Method
- Java ArrayList retainAll() Method
- Java ArrayList sort() Method
- Java ArrayList spliterator() Method
This example demonstrates the usage following ArrayList class methods:
- add(String e) - Appends the specified element to the end of this list (optional operation).
- get(int index) - Returns the element at the specified position in this list.
- set(int index, String element) - Replaces the element at the specified position in this list with the specified element (optional operation).
Java ArrayList add(), get() and set() Method Example
package com.javaguides.collections.arraylistexamples; import java.util.ArrayList; import java.util.List; public class AccessElementsFromArrayListExample { public static void main(String[] args) { List < String > topCompanies = new ArrayList < > (); // Check if an ArrayList is empty System.out.println("Is the topCompanies list empty? : " + topCompanies.isEmpty()); topCompanies.add("Google"); topCompanies.add("Apple"); topCompanies.add("Microsoft"); topCompanies.add("Amazon"); topCompanies.add("Facebook"); // Find the size of an ArrayList System.out.println("Here are the top " + topCompanies.size() + " companies in the world"); System.out.println(topCompanies); // Retrieve the element at a given index String bestCompany = topCompanies.get(0); String secondBestCompany = topCompanies.get(1); String lastCompany = topCompanies.get(topCompanies.size() - 1); System.out.println("Best Company: " + bestCompany); System.out.println("Second Best Company: " + secondBestCompany); System.out.println("Last Company in the list: " + lastCompany); // Modify the element at a given index topCompanies.set(4, "Walmart"); System.out.println("Modified top companies list: " + topCompanies); } }
Output
Is the topCompanies list empty? : true
Here are the top 5 companies in the world
[Google, Apple, Microsoft, Amazon, Facebook]
Best Company: Google
Second Best Company: Apple
Last Company in the list: Facebook
Modified top companies list: [Google, Apple, Microsoft, Amazon, Walmart]
Comments
Post a comment