✍️
Notes.md
  • Table of contents
  • React.Js
    • React Hooks
    • Old :- React : Using Classes
  • Blockchain
    • Solidity
    • Custom ERC20 token
    • Contract
  • Tools and Tech
    • Docker
    • Git version Control
  • Java
    • Data & Data Types
    • IO in Java
    • Data Structures
      • Array in Java
      • Collections in Java
      • Map in Java
      • Enums in Java
      • Linked List in Java
      • List in Java
      • Queues & Stacks
      • Set in Java
      • TreeSet and TreeMap
    • Object Oriented Programming
      • Object Class Methods and Constructor
      • Immutable Class & Objects
      • Constructors
      • Visibility
      • Generics
    • Threads in Java
    • Useful Stuff Java
      • Lambda & Stream
    • Keywords in Java
      • Annotations
      • Comparators
      • Packages in Java
    • Miscellaneous
    • Articles to refer to
  • Golang
    • Competitive Programming in Go
    • Testing simple web server
    • Learning Go : Part 1
    • Maps vs slices
    • Golang Garbage Collector 101
    • Things Golang do differently
    • Go Things
  • Linux
    • Shell programming
    • Linux Commands Part 1 - 4
    • Linux Commands Part 5 - 8
    • Linux Commands Part 9 - 10
  • Software Design
    • Solid Design
    • OOPS
    • Design Patterns
      • Creational Design Pattern
        • Builder DP
        • Factory DP
        • Singleton DP
      • Adapter DP
      • Bridge DP
      • Iterator DP
      • State DP
      • Strategy DP
      • Behavioral Design Pattern
        • Observer DP
      • Structural Design Pattern
        • Facade DP
  • Cloud
    • Google Cloud Platform
      • GCP Core Infrastructure
      • Cloud Networking
  • Spring Boot
    • Spring Basics
      • Spring Beans
      • Important Annotations
      • Important Spring Things
      • Maven Things
      • Spring A.O.P
    • Spring Boot Controller
      • Response Entity Exception Handling
    • Spring Things
    • Spring MVC
    • Spring Data
      • Redis
      • Spring Data JPA
      • JDBC
    • Apache Camel
  • Miscellaneous
    • Troubleshooting and Debugging
Powered by GitBook
On this page
  • Array List
  • Creating a List
  • Positional Access Operations
  • Iterating over the List
  • To add an element
  • To remove

Was this helpful?

  1. Java
  2. Data Structures

List in Java

Just like shopping list

PreviousLinked List in JavaNextQueues & Stacks

Last updated 3 years ago

Was this helpful?

imports

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

Array List

Array List is one of the inbuilt data structures in Java. It is a dynamic array (where the size of the data structure not needed to be declared first) for storing elements (Objects).

Creating a List

List<String> strings;
List<Double> doubles;

If you try to add something to the lists above you will get a NullPointerException, because strings and doubles, both equal null!

List<T> myArrayList = new ArrayList<>();
List<T> myLinkedList = new LinkedList<>();

Positional Access Operations

add(T type)
add(int index, T type)
remove(Object o)
remove(int index)
get(int index)
set(int index, E element)
int indexOf(Object o)
int lastIndexOf(Object o)

Iterating over the List

public void printEachElement(List<String> list){
    for(String s : list){
        System.out.println(s);
    }
}

To add an element

myArrayList.add(element);
myArrayList.add(index, element); 
//index of the element should be an int (starting from 0)

To remove

myArrayList.remove(element);
myArrayList.remove(index); 
//index of the element should be an int (starting from 0)
  • Removing elements from list B that are present in the list A

numbersB.removeAll(numbersA);
    System.out.println("B cleared: " + numbersB);
read more here
Class Diagram (click to zoom)