Arrays Programming Examples in Java - Part 1

List of Java Array Programs - Part 1
----------------------------------------------
1. Print Default Order Array
2. Print Reverse Order Array.
3. Find SUM of Array.
4. Find MIN of Array.
5. Find MAX of Array.
6. Sort Array in Ascending Order.
7. Sort Array in Descending Array.
-----------------------------------------------

 public class ArrayProgramsPart1 {

public static void main(String[] args) {

int[] a = { 50, 10, 70, 25, 99 };

printDefaultArray(a);
printReverseArray(a);
  sum(a);
min(a);
max(a);
sortArrayAscending(a);
sortArrayDescending(a);
}


public static void printDefaultArray(int[] a) {
System.out.println("****Print Default Array****");
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}


public static void printReverseArray(int[] a) {
System.out.println("****Print Reverse Array****");
for (int i = a.length - 1; i >= 0; i--) {
System.out.println(a[i]);
}
}

 public static void sum(int[] a) {
  System.out.println("****SUM of Array****");
int sum = 0;
 for (int i = 0; i < a.length; i++) {
sum = sum + a[i];
}
System.out.println(sum);
}

public static void min(int[] x) {
System.out.println("****MIN of Array****");
int min = x[0];

for (int i = 1; i < x.length; i++) {
if (x[i] < min)
min = x[i];
}
System.out.println(min);
}


public static void max(int[] y) {
System.out.println("****MAX of Array****");
int max = y[0];

for (int i = 1; i < y.length; i++) {
if (y[i] > max)
max = y[i];
}
System.out.println(max);
}

public static void sortArrayAscending(int[] a) {
int temp;

for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.println("****Ascending Array****");
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}

public static void sortArrayDescending(int[] a) {
int temp;

for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.println("****Descending Array****");
for (int i = a.length-1; i>=0; i--) {
System.out.println(a[i]);
}
}

}

Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. The very issue of programming is certainly interesting and if someone has a strict mind I think that he can become a programmer. For me, reading the blog https://grapeup.com/blog/ is completely enough for me because I know how much interesting information I can get from it. The more that I am an advanced user of applications running in the cloud.

    ReplyDelete

Post a Comment

Popular posts from this blog

Explain public static void main in Java

Reserve or Key Words in Java

What is Automation ?