Java / Arrays
1. How do I initialize a String array?
String myFruits[]; myFruits = new String[] { "Apple", "Pineapple", "Strawberry" }; or just in one line, String myFruits[] = new String[] { "Apple", "Pineapple", "Strawberry" }; more simpler, String[] myFruits = { "Apple", "Pineapple", "Strawberry" }; when passing as an...
2. Is arrays are considered as primitive data types?
3. Arrays.asList(arrayIdentifier)
Arrays.asList(array) returns a fixed-size List backed by the array. add and remove throw. Changes to the list write through to the array and the reverse.
4. How do I create a list from Array which is completely independent of the original array?
The fixed length list returned by asList method is used as an argument to create a new ArrayList which is later garbage collected. The new ArrayList is independent of the original array and the changes to the list does not...
5. What is the index of the first element in an array?
The index value of the first element is 0 in an Array.
6. How do you print the content of an array in Java?
In Java, arrays do not override toString() method, so the content cannot be printed just by specifying the array reference as illustrated below. int [] intArray = new int [] { 1 , 2 , 3 , 4 , 5...
7. How do you print the content of a multi dimensional array in Java?
java.util.Arrays.deepToString(Object[]) method returns a string representation of the content of the single (or) multi dimensioned array. The below example illustrates how the deepToString method could be used to print the content of a multi dimensional array....
8. Why is it a good practice to store sensitive information like password, SSN into a character Array rather than String?
The String objects are immutable and are stored in String pool in memory until garbage collected. So Although a string object is processed and no longer required, for an indeterminate period of time the string object remains in the memory...
9. What is the tradeoff between using an unordered array versus an ordered array?
The major advantage of an ordered array is that the search times have time complexity of O(log n), compared to that of an unordered array, which is O (n). The disadvantage of an ordered array is that the insertion operation...
10. Which algorithm does Arrays.sort use in Java?
Arrays.sort implementation uses merge sort or tim sort.
11. Difference between Arrays.sort and Arrays.parallelSort in Java 8.
Arrays.parallelSort is introduced in Java 1.8 and uses multi threading.
12. On which memory arrays are created in Java?
Arrays are created on dynamic memory by JVM.
13. Can we declare array size as negative?
No, it is not possible to declare array size as negative. We won't get any compile-time error however we will encounter NegativeArraySizeException at run-time.
14. Difference between int array[] and int[] array.
There is no difference between array[] and []array. Both array[] and []array are the ways to declare an array....
15. The default value of the array in Java.
When we create a new array, it is always initialized with the default values. The default values of the array are: Array data-type Default value byte, short, int, and long 0 float and double 0 Boolean false Object null
16. Define jagged array.
A jagged array is a multidimensional array in which member arrays are of different sizes. For example, int array[][]=new int[3][].
17. How to copy an array in Java?
We can create a copy of an array in two ways, the first one is manually by iterating over the array and the second one is by using the arrayCopy() method. Using the arrayCopy() method of the System class is...
18. Is it possible to make an array volatile?
Yes, we can make an array volatile in Java. But we only make the variable that is pointing to array volatile....
19. When ArrayIndexOutOfBoundsException occurs?
The ArrayIndexOutOfBoundsException occurs when the program tries to access the index of an array. The exception also occurs when the index is higher than the size of the array or the index is negative.
20. How to check an array contains values or not?
Java Arrays class provides two methods isExists() and contains() to check an array has elements or not. Both the methods return true if an array has elements else returns false.