Monday, April 28, 2025

Java OO Tutorial - ArrayList

An ArrayList is a class that provides an implementation of a dynamic array. It is part of the java.util package and is similar to a regular array, but with several additional features. Here are some key points to know about ArrayList:

  • An ArrayList is created with a default initial capacity, but the capacity can be increased as needed.
  • The size of an ArrayList can be changed dynamically as elements are added or removed.
  • An ArrayList can hold objects of any type, including custom-defined classes.
  • Elements in an ArrayList are stored in contiguous memory locations, just like a regular array.
  • An ArrayList supports several useful methods for adding, removing, and accessing elements, such as add(), remove(), get(), set(), size(), isEmpty(), and more.
import java.util.ArrayList;

public class Main {	
	
	public static void main(String[] args) {
		
		ArrayList<String> pLanguages = new ArrayList<String>();
		
		pLanguages.add("Lisp");
		pLanguages.add("Java");
		pLanguages.add("Python");
		pLanguages.add("C++");
		
		System.out.println(pLanguages); //get all elements
		System.out.println(pLanguages.get(0));	 //get by index
		
		pLanguages.set(0, "Prolog"); //change by index
		System.out.println(pLanguages);
		
		pLanguages.remove(0); //remove element
		System.out.println(pLanguages);
		
		pLanguages.clear(); //delete all elements
		System.out.println(pLanguages);
		
		pLanguages.add("Perl");
		System.out.println(pLanguages);
	}		
}

Here's an explanation of the code block by block: 

import java.util.ArrayList;

This line imports the ArrayList class from the java.util package, which is required to use the ArrayList in Java. 

public class Main {	
	
	public static void main(String[] args) {

This code block defines the Main class, which contains the main method that will be executed when the program is run. 

ArrayList<String> pLanguages = new ArrayList<String>();

This line creates an ArrayList of type String named pLanguages

pLanguages.add("Lisp");
pLanguages.add("Java");
pLanguages.add("Python");
pLanguages.add("C++");

These lines add some elements to the pLanguages list. 

System.out.println(pLanguages); //get all elements
System.out.println(pLanguages.get(0));	 //get by index

These lines demonstrate how to get the elements from the list. The first line prints all elements in the list, while the second line prints the element at index 0. 

pLanguages.set(0, "Prolog"); //change by index
System.out.println(pLanguages);

This line demonstrates how to modify an element in the list. It changes the element at index 0 from "Lisp" to "Prolog", and then prints the updated list. 

pLanguages.remove(0); //remove element
System.out.println(pLanguages);

This line demonstrates how to remove an element from the list. It removes the element at index 0 (which is now "Prolog" because we changed it in the previous step), and then prints the updated list. 

pLanguages.clear(); //delete all elements
System.out.println(pLanguages);

This line demonstrates how to delete all elements from the list. It calls the clear() method on the pLanguages list, which removes all elements, and then prints the updated list (which should be empty). 

pLanguages.add("Perl");
System.out.println(pLanguages);

This line demonstrates how to add a new element to the list. It adds the string "Perl" to the pLanguages list, and then prints the updated list.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...