Java Programming Tutorial - 26 - ArrayList

preview_player
Показать описание
How to use the ArrayList, included in the Java standard library.

This video covers the basics of the ArrayList. What it is, how it works and how to use it methods.

This video doesn't cover the algorithmic side of the ArrayList, just how to employ it.
Рекомендации по теме
Комментарии
Автор

Hi Andrew, Your videos help me great in studying Java. I am now preparing for exam in 10 days and really wanted to ask you some questions.
I can se that the lectures are from 2016 - 2018 and today we are 2021, but I hope you can see this comment and tell me if you can teach me some details? /Nina

ninaalexieva
Автор

import java.util.ArrayList;

public class Main {

public static void main(String[] args) {

String[] foodArray = {"apples", "peanut butter", "tooth paste", "chocolate", "pizza", "oranges"};

/* add element
add element at index
contains object, returns boolean
get an object at index
return index of an object, or -1 if not found
remove element at index
remove an element
get size
ensure capacity
trim to size
*/

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

System.out.println(list);
list.add("apples");
list.add("peanut butter");

System.out.println(list);

list.add(1, "tooth paste");
System.out.println(list);

list.add("chocolate");
list.add("pizza");
list.add("oranges");
System.out.println(list);







System.out.println("Size: " + list.size());
list.remove("oranges");

System.out.println("Size: " + list.size());
System.out.println(list);

list.remove(0);
System.out.println("Size: " + list.size());
System.out.println(list);


list.trimToSize();


}
}

UniProgrammer