Arrays in Java

Array units are consecutive


Why start with zero?

Since the elements are representing how far the distance is from the arrayName.


for arrays, you need to declare the array first, then fill it up, by creating a new array[number of elements].


The long line here is the preferred way for setting up all elements

int[] arr=new int[3];


Both are correct, whether you use a regular or final variable.

Also, even if you change the size of the variable later on, it doesn’t change the array size.


What is stored in the elements once the array is declared?

0 is stored when not initilaized.


2D Arrays



Who gives the error?

Java Virtual Machine, since there is no OS for Java.

Why use an array with only one element?

For filtering purposes, like a Minecraft hopper.


Traversing 1D array

With this, you can get the length instantly from an array with .length, and size and length are the same size.


It is by rows and columns, not by top, then bottom, essentially, don’t think cartesian with it.

.length is also gets you the number of rows, and compared to string arrays(char arrays) don’t use () afte length, while for char arrays, use () after length.


How to get the length of a column?

Get the first row, and ask for the length of that individual row, netting you a column.

arr[0].length;

Why the print?

So that you can print consecutively after each other for each row, then moving to another row using

System.out.println();

Why can this give a runTime error?

If J > I, then it can reach out of bounds.


Is this an array?

Yes, it’s a jagged array, and the regular setup works, since j goes until the

arr2d[i].length;

times.



With this, an array of strings is normal; even a 1D array of strings holds multiple strings, not like c, where 1D is just one word, and 2D is multiple words.


With String array, are you creating a 2D char array?

No

Why not?

Since strings are immutable, meaning you can’t change a string unless you want to make a new string to demonstrate the changes.

How to access it?

name.charAt(0);

With arrays, you can modify the values inside the array, also, since the changes are “live” like passing by reference, you don’t need to return the new array.


Library and USeful Methods for Arrays


Arrays.fill(ar,1,5,10);// will fill from index 1 to index 4
Arrays.fill(ar,10);// to fill complete arryas with a particular value

Sorts the array.

How does it sort automatically?

From the companies, < 200 uses Insertion sort, > 200 uses merge sort, name for it, idk. Like last year.


The Output is error, since I is inside the for loops, not outside the for loops, so it doesn’t exist.



Which level of copying?

Deep copy

What is the difference between Shallow and deep Copy?

Shallow copy means copying the references, just pointing to the same thing.

Deep copy means copying the actual contents and creating a true duplicate.


This is where copyOf comes in, since without it, you would have to write an function for it.

Yeah, when copying into a larger array, other elements not copied in is initialized to zero.


Comparing

for(int i: arr) to for(int i=0; i< arr.length; i++)

With shorthand, you aren’t modifying anything with the array, since really, all you are doing is traversing the array, nothing else. Think read-only, can’t edit, but with second method//preferred, it will be able to modifiy it.


With this fill method, it’s not possible to assign an array a function that returns arrays, so returning arrays is not it for here.


With last question, enhanced for loop is only for reading, while regular for loop is for reading, writing, changing, modifyling, etc.

If you assign an array to another one, it would modify both arrays. Both of them are already changed.


While you can use the parameter as a counter, it is weird code, and might hurt it down the line, or not, maybe, since it would be inside of the for loop.