![]() |
VOOZH | about |
An array is a group of elements with similar data types that are bound together as one. The array allows us to store and manipulate a collection of elements together. Mastering arrays is essential for any Java developer, as it provides a solid foundation for more complex data structures and algorithms. In this article, we will learn about Java Array with Java Practice Problems.
In this practice blog, we will dive into Java Array exercises to help you strengthen your Array skills. It is both beginner and experienced-friendly. So, if you are ready to tackle some Java array practice problems and take your coding skills to the next level, let's get started!
As we said above, Array is one of the most important and fundamental data structures in the Java programming language. So, in this section, we have covered all the basic Array-based Java exercises.
Input: Array: [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] Element: 50 Output: Array: [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 50 ] Explanation: Adding Element in the Array
Output:
[[ 1 , 2 , 3 ]
[ 4 , 5 , 6 ]
[ 7 , 8 , 9]]
Input: A[][] = [[ 1 , 2 ], [ 3 , 4 ]] B[][] = [[ 1 , 1 ], [ 1 , 1 ]] Output: Result = [[ 2 , 3 ] [ 4 , 5 ]] Explanation: Adding Elements of both the Matrices and Printing the Result.
Input: [ [ 1, 2, 3 ] [ 4, 5, 6 ] [ 7, 8, 9 ] ] Output: [ [ 1, 4, 7] [ 2, 5, 8] [ 3, 6, 9] ] Explanation: Transpose of a matrix is obtained by changing rows to columns and columns to rows.
Input: [ 1 , -3 , -2 ] [ -1 , 2 , 1 ] [ 1 , 0 , -2 ] Output: 3Explanation: 1*(-4-0) + 3*(2-1) + (-2)*(0-2) = 3
Input: arr[] = {1, 2, 3, 4, 5, 6, 7} , d = 3 Output: 4 5 6 7 1 2 3 Explanation: Taking the element from the beginning from the front and adding it to the end of the array
Expect challenges that involve manipulating, searching, and transforming arrays in more complex ways. This section will solidify your understanding and prepare you for advanced array concepts.
Input: [ 1 , 2 , 3 ] [ 4 , 5 , 6 ] [ 7 , 8 , 9 ] Output: [ 4 , 1 , 2 ] [ 7 , 5 , 3 ] [ 8 , 9 , 6 ] Explanation: We need to shift all the elements in Clockwise in the Matrix.
Input: [ 1 , 2 , -3 ] [ 4 , 5 , 6 ] [ 7 , 8 , -9 ] Output: Primary Diagonal: 9 Secondary Diagonal: -3 Explanation: Out of Two diagonal the higher sum is called primary diagonal that is (-3+5+7) and second one is called secondary diagonal(1+5-9).
Input : [ 1 , 2 , 3 , 4] [ 5 , 6 , 7 , 8 ] [ 9 , 10 , 11 , 12 ] [ 13 , 14 , 15 , 16 ] Output: 1 2 3 4 5 8 9 12 13 14 15 16
Input: arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5} Output: arr[] = {1, 2, 3, 4, 5} Explanation: All the elements in the array should be unique there should not be any duplicate element in array.
Input: array = { 10, 20, 10, 30, 50, 10 }, key = 10 Output: [20, 30, 50] Explanation: Removing element key(10) all occurrences from the array and then returning the array .
Input: arr1[] = [ 5, 8, 9 ] arr2[] = [ 4, 7, 10 ] Output: result[] = [ 5, 8, 9, 4, 7, 10 ] Explanation: Returning the array result which contains elements from arr1 and then elements from arr2 added in it.
Input: arr = [ [ 39, 27, 11, 42 ],
[ 10, 93, 91, 90 ],
[ 54, 78, 56, 89 ],
[ 24, 64, 20, 65 ] ];
col = 3
Output: [ [ 39 , 27 , 11 , 42 ],
[ 24 , 64 , 20 , 65 ],
[ 54 , 78 , 56 , 89 ],
[ 10 , 93 , 91 , 90 ] ];
Explanation:
Sorting the array according the col(3). So, all the rows are placed in such a way that column is sorted.
Input : Arr = {2, 6, 23, 98, 24, 35, 78} Output : [98, 78, 35, 24, 23, 6, 2] Explanation: All the elements are sorted in Descending order.
Input: [ 12 , 11 , 13 , 5 , 6 , 7 ] Output: [ 5 , 6 , 7 , 11 , 12 , 13 ] Explanation: Using Merge Sort to Sort the array. Time Complexity: O( N log(N) )
Have you got the DSA basics down? So get ready to dive deeper! Explore advanced concepts, troubleshooting techniques, or even interview questions to solidify your DSA knowledge and prepare for coding challenges.
Input: [[ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ], [ 13, 14, 15, 16 ]] Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Input: [ [ 1 , 1 , 1 ], [ 1 , 0 , 1 ], [ 1 , 1 , 1 ]] Output: [ [1, 0, 1], [0, 0, 0], [1, 0, 1]] Explanation: All the elements with the direct contact with 0 turns into 0.
Input: [ 0 , 1 , 2 , 0 , 1 , 2] Output: [ 0 , 0 , 1 , 1 , 2 , 2 ]
Input : arr[] = {1, 2, 0, 0, 0, 3, 6}; Output : arr[] = {1, 2, 3, 6, 0, 0, 0}; Explanation: Rearrange all the elements in such way that the sequence is not changed and zero.
To Practice Java Online please check our Practice Portal. <- Click Here
That's it for our Java Array exercise crash course! The exercises throughout this guide are your playground to experiment and solidify your skills in Java Array. Remember, practice makes perfect when it comes to arrays (and coding in general!). So bookmark this guide, revisit the exercises, and keep building awesome Java projects!