Before practicing in Leetcode and taking up any contest or interviews I refer to the below notes.
These notes contain basic details such as how to declare, initiate, access(add/remove), and iterate arrays.
๐๐ซ๐ซ๐๐ฒ๐ฌ
stores values of the same data type. Address of elements are stored consecutively in memory
length is fixed. inserting or deleting an element in middle is not easy.
๐๐๐๐ฅ๐๐ซ๐๐ญ๐ข๐จ๐ง:
int[] nums = {1,2,4} //initializing with values
int[] nums = new int[10] //initializing with size. 0 will be initialized in all indexes if we declare without variables.
๐๐๐๐๐ฌ๐ฌ๐ข๐ง๐ ๐๐ง ๐๐ฅ๐๐ฆ๐๐ง๐ญ:
Array index starts from 0โฆlength-1
int[] nums = {1,2,4}
nums[0] โ 1
nums[1] โ 2
nums[2] โ 4
๐๐จ๐ซ๐ญ๐ข๐ง๐ ๐๐ง ๐๐ฅ๐๐ฆ๐๐ง๐ญ:
๐๐ซ๐ซ๐๐ฒ๐ฌ.๐ฌ๐จ๐ซ๐ญ(๐ง๐ฎ๐ฆ๐ฌ);
Filling array element with some values:
Arrays.fill(nums, -1) โ this will assign all values in the array as -1
Binary search โ Arrays.binarySearch(array,value to find) โ returns index
๐๐ข๐ฌ๐ญ:
size is dynamic
- ๐๐ซ๐ซ๐๐ฒ๐๐ข๐ฌ๐ญ & ๐๐ข๐ง๐ค๐๐ ๐๐ข๐ฌ๐ญ
They store in the order we provide and it can have duplicates
List a1 = new ArrayList();
a1.add(โZaraโ);
a1.add(โMahnazโ);
a1.add(โAyanโ);
System.out.println(โ ArrayList Elementsโ);
System.out.print(โ\tโ + a1);
List l1 = new LinkedList<>();
l1.add(โZaraโ);
l1.add(โMahnazโ);
l1.add(โAyanโ);
System.out.println();
System.out.println(โ LinkedList Elementsโ);
System.out.print(โ\tโ + l1);
๐จ๐ฎ๐ญ๐ฉ๐ฎ๐ญ:
ArrayList Elements
[Zara, Mahnaz, Ayan]
LinkedList Elements
[Zara, Mahnaz, Ayan]
๐๐ข๐๐๐๐ซ๐๐ง๐๐ ๐๐๐ญ๐ฐ๐๐๐ง ๐๐ซ๐ซ๐๐ฒ ๐๐ข๐ฌ๐ญ & ๐๐ข๐ง๐ค๐๐ ๐๐ข๐ฌ๐ญ:
Array List stores values sequentially, so if we add or remove a value it needs more time to shift its elements and adjust
LinkedList stores randomly in memory and links by pointers. inserting and deleting takes less time here
๐ฆ๐๐ญ๐ก๐จ๐๐ฌ -
add(value), add(index, value),remove(value),get(key),
contains(value) returns true or false
isEmpty() return true or false,
clear(),
size() returns int
removeLast()
removeFirst()