Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
270 views

Learn Java - Arrays and ArrayLists Cheatsheet - Codecademy

Arrays are fixed in size and store elements of the same data type. An array's index starts from 0. To create an array, use {} notation or the new keyword. The value at a specific index can be accessed or changed. ArrayLists are dynamic lists that allow adding and removing elements. An ArrayList is created using new ArrayList<>() and elements are added/removed using add() and remove() methods. The remove() method can specify an index or element to remove.

Uploaded by

ilias ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
270 views

Learn Java - Arrays and ArrayLists Cheatsheet - Codecademy

Arrays are fixed in size and store elements of the same data type. An array's index starts from 0. To create an array, use {} notation or the new keyword. The value at a specific index can be accessed or changed. ArrayLists are dynamic lists that allow adding and removing elements. An ArrayList is created using new ArrayList<>() and elements are added/removed using add() and remove() methods. The remove() method can specify an index or element to remove.

Uploaded by

ilias ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Cheatsheets / Learn Java

Arrays and ArrayLists


Arrays
In Java, an array is used to store a list of elements of the
same datatype. // Create an array of 5 int elements
Arrays are fixed in size and their elements are ordered. int[] marks = {10, 20, 30, 40, 50};

Array creation in Java


In Java, an array can be created in the following ways:
int[] age = {20, 21, 30};
● Using the {} notation, by adding each element all
at once.
int[] marks = new int[3];
● Using the new keyword, and assigning each marks[0] = 50;
position of the array individually. marks[1] = 70;
marks[2] = 93;

Index
An index refers to an element’s position within an array.
The index of an array starts from 0 and goes up to one int[] marks = {50, 55, 60, 70, 80};
less than the total length of the array.
System.out.println(marks[0]);
// Output: 50

System.out.println(marks[4]);
// Output: 80

Changing an Element Value


To change an element value, select the element via its
index and use the assignment operator to set a new value. int[] nums = {1, 2, 0, 4};
// Change value at index 2
nums[2] = 3;
Java ArrayList
In Java, an ArrayList is used to represent a dynamic list.
While Java arrays are fixed in size (the size cannot be // import the ArrayList package
modified), an ArrayList allows flexibility by being able to import java.util.ArrayList;
both add and remove elements.

// create an ArrayList called students


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

Modifying ArrayLists in Java


An ArrayList can easily be modified using built in
methods. import java.util.ArrayList;
To add elements to an ArrayList , you use the add()
method. The element that you want to add goes inside of public class Students {
the () .   public static void main(String[] args) {
To remove elements from an ArrayList , you use the
    
remove() method. Inside the () you can specify the
     // create an ArrayList called
index of the element that you want to remove.
Alternatively, you can specify directly the element that
studentList, which initially holds []
you want to remove.         ArrayList<String> studentList
= new ArrayList<String>();
    
    // add students to the ArrayList
    studentList.add("John");
    studentList.add("Lily");
    studentList.add("Samantha");
    studentList.add("Tony");
    
    // remove John from the ArrayList,
then Lily
    studentList.remove(0);
    studentList.remove("Lily");
    
    // studentList now holds [Samantha,
Tony]
    
    System.out.println(studentList);
  }
}

You might also like