Arrays, Java inbuilt Lists, Autoboxing and Unboxing Flashcards
Array
- a data structure that allows you to store multiple values of the same type into a single variable
- the default values of numeric array elements are set to zero
- arrays are zero-indexed
Explain code:
int [ ] reference = new int [number]
- new keyword is used to create the array and initialize the array elements to their default values
Explain code:
scanner. nextInt();
scanner. nextLine();
when you have the nextInt() method, it only consumes the number and leaves the "enter" behind (in the buffer). The nextLine() method will clear that "enter" from the buffer first.
ArrayIndexOutOfBoundsException
xxx
[ ]
square brackets [ ] would indicate an array
{ }
- code block
- array initializer block
Reference type vs Value type
- the primitive data types are predefined
- the reference data type refers to where data is stored. They contain the reference/addresses of dynamically created objects.
- once we create a reference variable, they only store the address of these values.
Write the general syntax to create and declare an ArrayList object
ArrayList < type > reference = new ArrayList<> ();
- the type can be primitive like int, double, boolean
- it also can be String, the objects you create
ArrayList
- the ArrayList class is a resizable array.
- it can be found in java.util package
Write the general syntax to declare, declare and instantiate, declare and instantiate and initialize an array
type [ ] reference;
type [ ] reference = new type [size];
type [ ] reference = new type [size] {array literal}
autoboxing
automatic conversion of primitive types to the object of their corresponding wrapper class
unboxing
- the reverse process of autoboxing
- automatically converting an object of a wrapper class to its corresponding primitive type
LinkedList
- a linear data structure that contains a sequence of nodes, in which each node contains data and a pointer (reference) to another node.
- singly linked list and doubly linked list
- to access our desired value, we would have to traverse the linked list and continue accessing the next value of each node until arriving at it
Write the syntax of enhanced for loop in Java?
for (declaration: expression) { // statements }
How to create an ArrayList instance?
ArrayList < type > reference = new ArrayList < >;