Prev Next

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 argument to a method, metho...

Read full answer

2. Is arrays are considered as primitive data types?

Posted on Apr 1, 2016 by Senthil Kumar.

No, Arrays are objects in Java.

Read full answer

3. Arrays.asList(arrayIdentifier)

asList method takes an array/vararg argument and returns a fixed-size list backed by the specified array. Any modification or overwrite applied on the list will reflect on the original array and like wise. Remember that asList is fixed size so we can not add/remove new elements to the List. Rearr...

Read full answer

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 reflect on the array and vice versa. Adding/removing elements could be per...

Read full answer

5. What is the index of the first element in an array?

The index value of the first element is 0 in an Array.

Read full answer

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 }; System.out.println(intArray); // prints something like '[I@1732a4df' char [] charArray = new char []...

Read full answer

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. // initializing an object array Obje...

Read full answer

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 until garbage collected. Even this can not be controlled p...

Read full answer

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 has a time complexity of O(n), because the elements with higher values mu...

Read full answer

10. Which algorithm does Arrays.sort use in Java?

Arrays.sort implementation uses merge sort or tim sort.

Read full answer

11. Difference between Arrays.sort and Arrays.parallelSort in Java 8.

Arrays.parallelSort is introduced in Java 1.8 and uses multi threading.

Read full answer

12. On which memory arrays are created in Java?

Arrays are created on dynamic memory by JVM.

Read full answer

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.

Read full answer

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. The only difference between them is that if we are declaring more than one array in a line, we should use prefix []. If we are declaring a single array in a line, we should use postfix [...

Read full answer

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

Read full answer

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][]. The statement creates a two-dimensional jagged array.

Read full answer

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 the fastest way to copy an array and also allows us to copy a part of the array. You may a...

Read full answer

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. If an array is changed by replacing individual elements that happen before the guarantee provided by volatile variables will not hold.

Read full answer

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.

Read full answer

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.

Read full answer

«
»

Comments & Discussions