Arrays Programming Examples in Java - Part 2
List of Java Array Programs - Part 2
-----------------------------------
8. Find the Size of an Array.
9. Find Even and Odd in Array.
10. Find PRIME number in Array.
11. Increase each Array elements size by 1.
12. Check Array contains the specific value.
13. Create Array List from Array.
14. Find Duplicate Using Brute Force Method.
15. Find Duplicate Using HashSet.
16. Remove Duplicates in Array using Set (HashSet).
-----------------------------------
public class ArrayProgramsPart2 {
public static void main(String[] args) {
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
String[] s = { "Java", "C", "Java", "C", "NODDY", "SELENIUM", "AUTOTECHX" };
SizeOfArray(a);
EvenOdd(a);
PrimeNumber(a);
IncreaseArrayBy1(a);
ArrayContains(s);
CreateArrayListFromArray(s);
FindDuplicateUsingBruteForceMethod(s);
FindDuplicateUsingHashSet(s);
RemoveDuplicates(s);
}
public static void SizeOfArray(int[] a) {
System.out.println("Size of Array: " + a.length);
}
public static void EvenOdd(int[] a) {
for (int i = 0; i < a.length; i++) {
if (a[i] % 2 == 0) {
System.out.println("Even Numbers in Array: " + a[i]);
} else if (a[i] % 2 != 0) {
System.out.println("Odd Numbers in Array: " + a[i]);
}
}
}
public static void PrimeNumber(int[] a) {
for (int i = 0; i < a.length; i++) {
boolean isPrime = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime)
System.out.println(i + ": is the Prime Number");
}
}
public static void IncreaseArrayBy1(int[] a) {
for (int i = 0; i < a.length; i++) {
a[i]++;
System.out.print(+a[i] + ",");
}
}
public static void ArrayContains(String[] s) {
boolean b = Arrays.asList(s).contains("c");
System.out.println(b);
}
public static void CreateArrayListFromArray(String[] s) {
ArrayList<String> alist = new ArrayList<String>(Arrays.asList(s));
System.out.println(alist);
}
public static void FindDuplicateUsingBruteForceMethod(String[] s) {
for (int i = 0; i < s.length - 1; i++) {
for (int j = i + 1; j < s.length; j++) {
if ((s[i].equals(s[j])) && (i != j)) {
System.out.println("Duplicate Element is : " + s[j]);
}
}
}
}
public static void FindDuplicateUsingHashSet(String[] s) {
HashSet<String> set = new HashSet<String>();
for (String s1 : s) {
if (!set.add(s1)) {
System.out.println("Duplicate Element is : " + s1);
}
}
}
public static void RemoveDuplicates(String[] s) {
Set<String> sa = new HashSet<String>(Arrays.asList(s));
System.out.println(sa);
}
}
core java examples to learn
ReplyDeleteFinding the maximum of three doubles