6.What is the difference between Arrays.sort() and manually sorting an array in Java?
Arrays.sort() is an in-built Java method which sorts the arrays with the help of optimized algorithms, like Timsort.
The function is pretty easy to use and efficient in most sorting applications.
For example, Arrays.sort(arr);
would sort an array in ascending order.
For manually sorting an array, one would have to write his/her own logic for the sort, perhaps by implementing the logic for bubble sort or quick sort.
It has flexibility with custom criteria but requires a lot more code than optimized Arrays.sort().
7.How can you copy an array in Java?
In Java, you can copy an array in several ways. The simplest method is using Arrays.copyOf(), which creates a new array and copies the elements into it.
For example,
int[] newArr = Arrays.copyOf(arr, arr.length);
creates a new array newArr with the same elements as arr.
Alternatively, you can use the System.arraycopy(), which gives a better control in the copying operation, such as source and destination indices.
The last way to copy an array is using a loop, though such methods are slower than the methods provided by this class.
8.What is Arrays.copyOf() and Arrays.copyOfRange() in Java?
Arrays.copyOf() in Java is used for creating a new array that is copy of the given original array and possibly with some new size, as well. It copies all the elements from the source array to another destination array or it can make the new one with the given specified length with same length to the source. If the new length is more than the current array's length then new elements are filled with default values.
For example:
int[] newArr = Arrays.copyOf(arr, 10);
creates a new array of size 10.
Arrays.copyOfRange() works similarly, but you can specify a range of elements to copy.
For example,
Arrays.copyOfRange(arr, 2, 5); copies the elements from index 2 up to index 4.
9.How do you convert an array to a List or vice versa in Java?
To convert an array to a List in Java, you can use Arrays.asList().
This method returns a fixed-size list backed by the array.
For example:
List<Integer> list = Arrays.asList(arr);
converts an array to a List. However, modifying this list (e.g., adding or removing elements) is not allowed. You can use the toArray() method to convert a List into an array as follows:
T[] arr = list.toArray(new T[0]);
here T is the type of the List. A new array from the List is thus generated.
10.How can you find if a given array has an element with specific content in Java?
In Java, one would check to see if a particular element was present in an array by first using Arrays.asList() and the contains() method in case that the array had been converted into a List.
Here is the code:
List<Integer> list = Arrays.asList(arr); and list.contains(5);
this is checking whether the number 5 is contained within the array.
Alternatively, one could hand-check each value within the array as follows:
for (int num : arr)
{
if (num == 5)
{
return true;
}
}
11.How do you find the maximum and minimum values in an array in Java?
You can find the maximum or minimum values in an array by using a simple loop that compares each element.
For max, initialize a variable max with the first element of the array, then iterate through the array to find larger values:
int max = arr[0];
for (int i = 1; i < arr.length; i++)
{
if (arr[i] > max)
{
max = arr[I];
}
}
Similarly, to get the minimum, assign min to be the first element and scan for smaller elements. You could also use Arrays.stream(arr).max() for max and Arrays.stream(arr).min() for the min value if using streams in Java 8 or later.
12.What is the difference between an array and an ArrayList in Java?
In Java, the array is a fixed-size data structure to store the elements of the same data type. Once an array is created, its size cannot be changed.
For instance,
int[] arr = new int[5];
declares an array of size 5. ArrayList is a part of the Java Collections Framework and represents a dynamic array that expands or shrinks depending on the operations performed. It provides methods like add(), remove(), and get() for performing flexible operations on elements. Arrays are much faster in terms of performance but lack flexibility of ArrayList which is more apt for dynamic data.
13.How would you implement a dynamic array in Java without using the ArrayList class?
To implement a dynamic array in Java without using ArrayList, you can manually resize an array when it's full. You would typically create a new array with a larger size, copy the elements from the old array to the new array and then add the new element.
For example:
int[] arr = new int[initialSize];
int newSize = arr.length * 2; // doubling size
int[] newArr = new int[newSize];
System.arraycopy(arr, 0, newArr, 0, arr.length);
arr = newArr; /* Update reference to new array*/
This is how ArrayList doubles its internal array.
14.How do you iterate over an array with a for loop and an enhanced for loop in Java?
To iterate over an array using a plain for loop, you use the index to get each element:
for (int i = 0; i < arr.length; i++)
{
System.out.println(arr[i]);
}
OR The enhanced for loop is much shorter and doesn't need the indexing.
It can iterate over elements directly:
for (int num : arr)
{
System.out.println(num);
}
These two forms are used extensively; the enhanced form is preferred especially when there's no need to use an index.
15. How do you pass an array as a method parameter in Java?
You can pass an array to the method with specifying the type of array, by including an empty pair of brackets after the method's data type in its method signature as illustrated below:
public void printArray(int[] arr)
{
for (int num : arr)
{
System.out.println(num);
}
}
You can pass an array to this method by calling it like printArray(arr);. Java passes arrays by reference, so any changes made to the array inside the method will affect the original array.
16. How to find the index of an element in an array?
To find the index of an element in an array, we use a for loop to loop over the elements and compare them to the target value. The index of that particular element will be returned once we find the matching value.
Let's look at an example
int[] arr = {1, 2, 3, 4, 5};
int target = 3;
for (int i = 0; i < arr.length; i++)
{
if (arr[i] == target) {
System.out.println("Element found at index: " + i);
break;
}
}
Or if you are on Java 8 or later, you can make use of Arrays.asList() in combination with indexOf() to locate the index of an element in a list-backed array.
17. How do you eliminate an element in an array from Java?
There is no facility to remove the element from arrays in Java since arrays are size-fixed. What you can do is create another array excluding a particular element for which you would like to get rid of in your program. You can include all elements placed before and after the target one in the new array.
Suppose:
int[] arr = {1, 2, 3, 4, 5};
int target = 3;
int[] newArr = new int[arr.length - 1];
int j = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i]!= target) {
newArr[j++] = arr[i];
}
}
System.out.println(Arrays.toString(newArr));
This method effectively "removes" the element by creating a new array.
18. How do you merge two arrays in Java?
To merge two arrays in Java, you would have to first declare a new array whose size will be the sum of the lengths of both arrays and then copy all elements from the both arrays to this new declared array.
Example is as below.
int[] arr1 = {1, 2, 3};
int[] arr2 = {4, 5, 6};
int[] mergedArr = new int[arr1.length + arr2.length];
System.arraycopy(arr1, 0, mergedArr, 0, arr1.length);
System.arraycopy(arr2, 0, mergedArr, arr1.length, arr2.length);
System.out.println(Arrays.toString(mergedArr));
This approach combines both arrays into a single new array.
19. How do you find duplicate elements in an array in Java?
To find duplicate elements in an array, you can use a HashSet to store elements that have already been encountered. If an element is already in the set, it is a duplicate.
Here's an example:
int[] arr = {1, 2, 3, 4, 5, 3, 2};
Set<Integer> seen = new HashSet<>();
for (int num : arr) {
if (!seen.add(num)) {
System.out.println("Duplicate element found: " + num);
}
}
This method efficiently identifies duplicates by taking advantage of HashSet's properties.
20. How do you reverse an array in Java?
To reverse an array in Java, you can use a loop to swap elements from the front and back of the array.
Here's an example:
int[] arr = {1, 2, 3, 4, 5};
int start = 0;
int end = arr.length - 1;
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp
;
start++;
}
end--;
}
System.out.println(Arrays.toString(arr));
This in-place algorithm swaps the elements and reverses the array efficiently.
Previous Topic==> Java I/O FAQ. || Next Topics==> Java String Interview Questions
Other Topic==>
Banking Account Management Case Study in SQL
Top SQL Interview Questions
Employee Salary Management SQL FAQ!. C FAQ
Top 25 PL/SQL Interview Questions
Joins With Group by Having
Equi Join
Joins with Subqueries
Self Join
Outer Join