Array Flashcards
What is an Array?
- Array is a collection of similar data types. It can not have different data type. It can hold both primitive types (int, float, double) and object references.
- It is fixed in length i.e static in nature.
- Arrays are created on the heap memory not on the stack.
- Accessing an invalid index of an Array will cause exception.
How do you declare an Array in java?
You can declare an Array in java by the following way :
dataType[] arrayVariableName = new dataType[arraySize];
for example for int data type, you can declare an int array as :
int[] temp = new int[256]
Can you declare an Array without Array size?
No, you can not declare Array without Array size. You will get compile time error.
Where does Array stored in JVM memory ?
Array is an object in java. So, Array is stored in heap memory in JVM.
What are the advantages of Array ?
a. We can sort multiple elements of Array at the same time.
b. Using index, we can access the element of the Array in O(1) time.
What are the disadvantages of Array?
a. To create an Array, contiguous memory is required. It is possible in JVM that the memory is available to accommodate Array but memory available is not contiguous.
b. The Array is static data structure. It is of fixed size. We can not increase or decrease the size of the Array after creation.
Which is legal int[] arr or int arr[] ?
Both are legal statements. It is preferred to write int[] arr instead of int arr[].
What is the difference between Array and ArrayList ?
Array is static in size i.e of fixed length. Size can not be changed after declaration. ArrayList is dynamic in nature. If you add elements to an ArrayList, it will automatically increase its size.
Array can contain both primitive and Object data types. ArrayList does not contain primitive data types. It only contains object entries.
What is the difference between Array and LinkedList in java ?
Memory required for storing the same number of elements in Array is less as compared to LinkedList. Array only stores the data of the element whereas LinkedList stores data plus the address of the next node.
Array requires contiguous memory allocation where as LinkedList elements are present all over the heap memory. Unlike Array, LinkedList does not have limitation of contiguous memory.
What are jagged arrays in java?
Arrays containing arrays of different length is known as jagged arrays. Multidimensional arrays are also known as jagged arrays.
What are the different ways to copy one Array from another Array?
There are four ways by which we can copy an Array.
a. By using for loop
b. By using clone() method
c. By using Arrays.copyOf() method
d. By using System.arraycopy() method
What will happen if you do not initialize an Array?
Array will take default value depending upon the data type.
How to find duplicate elements in a given Array?
There are many ways by which you can find the duplicates in an Array. I am sharing two ways
a. using for loop and compare
b. using HashSet
Are Array thread-safe ?
Reading an Array is a thread-safe operation but modifications are not.
Find out smallest and largest number in a given Array?
Logic to find the smallest and largest number in a given Array is given below :
a. Create two variables for storing largest and smallest number.
b. Initialize smallest variable with value Integer.MAX_VALUE
c. Initialize largest variable with value Integer.MIN_VALUE
d. In each traversal of for loop, we will compare the current element with the largest and smallest number. We will update the value.
e. If a number is larger than largest, then it can not be smaller than the smallest. So we can skip if first condition is true.