1
0

Using One Dimensional Array in Java

So far, we have discussed the declaration, creation, and initialization of one-dimensional arrays separately. Let’s now write a few programs using the one-dimensional array.

In Java, all arrays store the allocated size in a variable named length. We can access the length of the array using number.length in the case of the above program. For example:

int size = number.length;

Here, in the above variable size, the length of the array is stored for manipulation of arrays.

......code from the previous program......
for(i=0; i<number.length; i++)
{
    System.out.print(number[i]+"\t")
}
........
........

Program to search an element in an Array

class LinearSearch
{
    public static void main(String args[])
    {
        int Num[] = {3, 9,18,7,2,5,2,11,2,37};
        int n, i, flag=0;
        n=2;        // Search Number
        System.out.println("Given numbers are....\n");
        for(i=0; i<Num.length; i++)
        {
            System.out.print(Num[i] + "\t");
        }
        for(i=0; i<Num.length; i++)
        {
            if(Num[i] == n)
            {
                flag=1;
                System.out.println("\nNumber found at index " + i);
                break;
            }
        }
        if(flag==0)
        {
            System.out.println("\nNumber Not Found");
        }
    }
}

The output of the above program:

Given numbers are....
3	9	18	7	2	5	2	11	2	37	
Number found at index 4

Program to sort an array of numbers using the Bubble Sort method

public class BubbleSortExample {
    static void bubbleSort(int[] arr) {
        int n = arr.length;
        int temp = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 1; j < (n - i); j++) {
                if (arr[j - 1] > arr[j]) {
                    //swap elements  
                    temp = arr[j - 1];
                    arr[j - 1] = arr[j];
                    arr[j] = temp;
                }

            }
        }

    }
    public static void main(String[] args) {
        int arr[] = {
            3,
            60,
            35,
            2,
            45,
            320,
            5
        };

        System.out.println("Array Before Bubble Sort");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();

        bubbleSort(arr); //sorting array elements using bubble sort  

        System.out.println("Array After Bubble Sort");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }

    }
}

The output of the above program is:

Array Before Bubble Sort
3 60 35 2 45 320 5 
Array After Bubble Sort
2 3 5 35 45 60 320 

Program to Reverse an Array of Numbers

public class reverseArray {

	// function that reverses array and stores it
	// in another array
	static void reverse(int a[], int n)
	{
		int[] b = new int[n];
		int j = n;
		for (int i = 0; i < n; i++) {
			b[j - 1] = a[i];
			j = j - 1;
		}

		// printing the reversed array
		System.out.println("Reversed array is: \n");
		for (int k = 0; k < n; k++) {
			System.out.println(b[k]);
		}
	}

	public static void main(String[] args)
	{
		int [] arr = {10, 20, 30, 40, 50};
		reverse(arr, arr.length);
	}
}

Output:

Reversed array is: 

50
40
30
20
10

Leave a Reply