w3resource
w3resource logo
Java Programming Exercises

Java Array: Exercises, Practice, Solution

Java Array Exercises [53 exercises with solution]

1. Write a Java program to sort a numeric array and a string array. Go to the editor

Click me to see the solution

2. Write a Java program to sum values of an array. Go to the editor

Click me to see the solution

3. Write a Java program to print the following grid. Go to the editor

Expected Output :

- - - - - - - - - -                                                                                           
- - - - - - - - - -                                                                                           
- - - - - - - - - -                                                                                           
- - - - - - - - - -                                                                                           
- - - - - - - - - -                                                                                           
- - - - - - - - - -                                                                                           
- - - - - - - - - -                                                                                           
- - - - - - - - - -                                                                                           
- - - - - - - - - -                                                                                           
- - - - - - - - - -  

Click me to see the solution

4. Write a Java program to calculate the average value of array elements. Go to the editor

Click me to see the solution

5. Write a Java program to test if an array contains a specific value. Go to the editor

Click me to see the solution

6.Write a Java program to find the index of an array element. Go to the editor

Click me to see the solution

7. Write a Java program to remove a specific element from an array. Go to the editor

Click me to see the solution

8. Write a Java program to copy an array by iterating the array. Go to the editor

Click me to see the solution

9. Write a Java program to insert an element (specific position) into an array. Go to the editor

Click me to see the solution

10. Write a Java program to find the maximum and minimum value of an array. Go to the editor

Click me to see the solution

11. Write a Java program to reverse an array of integer values. Go to the editor

Click me to see the solution

12. Write a Java program to find the duplicate values of an array of integer values. Go to the editor

Click me to see the solution

13. Write a Java program to find the duplicate values of an array of string values. Go to the editor

Click me to see the solution

14. Write a Java program to find the common elements between two arrays (string values). Go to the editor

Click me to see the solution

15. Write a Java program to find the common elements between two arrays of integers. Go to the editor

Click me to see the solution

16. Write a Java program to remove duplicate elements from an array. Go to the editor

Click me to see the solution

17. Write a Java program to find the second largest element in an array. Go to the editor

Click me to see the solution

18. Write a Java program to find the second smallest element in an array. Go to the editor

Click me to see the solution

19. Write a Java program to add two matrices of the same size. Go to the editor

Click me to see the solution

20. Write a Java program to convert an array to ArrayList. Go to the editor

Click me to see the solution

21. Write a Java program to convert an ArrayList to an array. Go to the editor

Click me to see the solution

22. Write a Java program to find all pairs of elements in an array whose sum is equal to a specified number. Go to the editor

Click me to see the solution

23. Write a Java program to test the equality of two arrays. Go to the editor

Click me to see the solution

24. Write a Java program to find a missing number in an array. Go to the editor

Click me to see the solution

25. Write a Java program to find common elements from three sorted (in non-decreasing order) arrays. Go to the editor

Click me to see the solution

26. Write a Java program to move all 0's to the end of an array. Maintain the relative order of the other (non-zero) array elements. Go to the editor

Click me to see the solution

27. Write a Java program to find the number of even and odd integers in a given array of integers. Go to the editor

Click me to see the solution

28. Write a Java program to get the difference between the largest and smallest values in an array of integers. The length of the array must be 1 and above. Go to the editor

Click me to see the solution

29. Write a Java program to compute the average value of an array of integers except the largest and smallest values. Go to the editor

Click me to see the solution

30. Write a Java program to check if an array of integers without 0 and -1. Go to the editor

Click me to see the solution

31. Write a Java program to check if the sum of all the 10's in the array is exactly 30. Return false if the condition does not satisfy, otherwise true. Go to the editor

Click me to see the solution

32. Write a Java program to check if an array of integers contains two specified elements 65 and 77. Go to the editor

Click me to see the solution

33. Write a Java program to remove the duplicate elements of a given array and return the new length of the array.
Sample array: [20, 20, 30, 40, 50, 50, 50]
After removing the duplicate elements the program should return 4 as the new length of the array.  Go to the editor

Click me to see the solution

34. Write a Java program to find the length of the longest consecutive elements sequence from a given unsorted array of integers.
Sample array: [49, 1, 3, 200, 2, 4, 70, 5]
The longest consecutive elements sequence is [1, 2, 3, 4, 5], therefore the program will return its length 5.  Go to the editor

Click me to see the solution

35. Write a Java program to find the sum of the two elements of a given array which is equal to a given integer.
Sample array: [1,2,4,5,6]
Target value: 6.  Go to the editor

Click me to see the solution

36. Write a Java program to find all the unique triplets such that sum of all the three elements [x, y, z (x ≤ y ≤ z)] equal to a specified number.
Sample array: [1, -2, 0, 5, -1, -4]
Target value: 2.  Go to the editor

Click me to see the solution

37. Write a Java program to create an array of its anti-diagonals from a given square matrix.  Go to the editor

Click me to see the solution

Example:
Input :
1 2
3 4
Output:
[
[1],
[2, 3],
[4]
]

38. Write a Java program to get the majority element from an given array of integers containing duplicates.  Go to the editor
Majority element: A majority element is an element that appears more than n/2 times where n is the size of the array.

Click me to see the solution

39. Write a Java program to print all the LEADERS in the array.   Go to the editor
Note: An element is leader if it is greater than all the elements to its right side.

Click me to see the solution

40. Write a Java program to find the two elements from a given array of positive and negative numbers such that their sum is closest to zero.   Go to the editor

Click me to see the solution

41. Write a Java program to find smallest and second smallest elements of a given array.   Go to the editor

Click me to see the solution

42. Write a Java program to segregate all 0s on left side and all 1s on right side of a given array of 0s and 1s.   Go to the editor

Click me to see the solution

43. Write a Java program to find all combination of four elements of an given array whose sum is equal to a given value.   Go to the editor

Click me to see the solution

44. Write a Java program to count the number of possible triangles from an given unsorted array of positive integers.   Go to the editor

Click me to see the solution

45. Write a Java program to cyclically rotate a given array clockwise by one.   Go to the editor

Click me to see the solution

46. Write a Java program to check whether there is a pair with a specified sum of a given sorted and rotated array.   Go to the editor

Click me to see the solution

47. Write a Java program to find the rotation count in a given rotated sorted array of integers.   Go to the editor

Click me to see the solution

48. Write a Java program to arrange the elements of an given array of integers where all negative integers appear before all the positive integers.   Go to the editor

Click me to see the solution

49. Write a Java program to arrange the elements of an given array of integers where all positive integers appear before all the negative integers.   Go to the editor

Click me to see the solution

50. Write a Java program to sort an array of positive integers of an given array, in the sorted array the value of the first element should be maximum, second value should be minimum value, third should be second maximum, fourth second be second minimum and so on.   Go to the editor

Click me to see the solution

51. Write a Java program to separate 0s on left side and 1s on right side of an array of 0s and 1s in random order.   Go to the editor

Click me to see the solution

52. Write a Java program to separate even and odd numbers of an given array of integers. Put all even numbers first, and then odd numbers.   Go to the editor

Click me to see the solution

53. Write a Java program to replace every element with the next greatest element (from right side) in an given array of integers.   Go to the editor

Click me to see the solution

Java Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.