Join Regular Classroom : Visit ClassroomTech

Data Structure – codewindow.in

Related Topics

Data Structure

dataType[] arrayName = new dataType[arraySize];

Here, dataType specifies the type of data that the array will hold, arrayName is the name of the array, and arraySize is the number of elements that the array can hold.

For example, to declare and initialize an array of integers that can hold five elements, you can use the following code:

int[] myArray = new int[5];

This creates an array called myArray that can hold five integers. The elements of the array are initialized to the default value of the data type, which is 0 for int in this case.

You can also initialize the array with specific values at the time of declaration using the following syntax:

dataType[] arrayName = {value1, value2, ..., valueN};

For example, to declare and initialize an array of integers with specific values, you can use the following code:

int[] myArray = {1, 2, 3, 4, 5};

This creates an array called myArray that contains the values 1, 2, 3, 4, and 5.

int[] myArray = {1, 2, 3, 4, 5};
int length = myArray.length; // returns 5

In the example above, the length variable is assigned the length of the myArray array using the length property.

arrayName[index]

Here, arrayName is the name of the array, and index is the index of the element that you want to access.

For example, let’s say you have an integer array called numbers and you want to access the element at index 3. You can do so using the following code:

int[] numbers = {1, 2, 3, 4, 5};
int element = numbers[3];
System.out.println(element); // Output: 4

In this code, we declare an integer array called numbers and initialize it with some values. Then, we access the element at index 3 using the square bracket notation and assign it to the element variable. Finally, we print the value of element to the console.

// Create an array of size 5
int[] originalArray = {1, 2, 3, 4, 5};

// Create a new array with size 6
int[] newArray = new int[6];

// Copy the elements from the original array to the new array
for (int i = 0; i < originalArray.length; i++) {
    newArray[i] = originalArray[i];
}

// Add a new element to the end of the new array
newArray[5] = 6;

In this example, we first create an array called originalArray with size 5 and initialize it with some values. We then create a new array called newArray with size 6, which is one element larger than the original array. We use a for loop to copy the elements from the original array to the new array, and then add a new element to the end of the new array. The result is a new array that has all the elements of the original array plus one new element at the end.

// Declare and initialize an array
int[] array = { 1, 2, 3, 4, 5 };

// Specify the index of the element to remove
int indexToRemove = 2;

// Create a new array with one less element
int[] newArray = new int[array.length - 1];

// Copy the elements from the original array to the new array
// Skip the element at the specified index
for (int i = 0, j = 0; i < array.length; i++) {
    if (i != indexToRemove) {
        newArray[j] = array[i];
        j++;
    }
}

// The element at the specified index has been removed
// newArray now contains { 1, 2, 4, 5 }

In this example, we declare and initialize an array array with five elements. We then specify the index of the element we want to remove (2, which corresponds to the value 3). We create a new array newArray with one less element than the original array. We then copy the elements from the original array to the new array, skipping the element at the specified index. Finally, newArray contains the remaining elements of array with the specified element removed.

public static void reverseArray(int[] arr) {
    int start = 0;
    int end = arr.length - 1;
    while (start < end) {
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        start++;
        end--;
    }
}

In this implementation, we use two pointers to iterate through the array. The start pointer starts at the first element of the array, and the end pointer starts at the last element of the array. In each iteration of the loop, we swap the elements at start and end, and then increment start and decrement end. We repeat this process until start is greater than or equal to end, which means we have reached the middle of the array. After this loop completes, the array will be reversed.

import java.util.Arrays;

public class SortExample {
    public static void main(String[] args) {
        int[] numbers = {5, 3, 8, 1, 4};
        
        Arrays.sort(numbers);
        
        for (int number : numbers) {
            System.out.print(number + " ");
        }
    }
}

Output:

1 3 4 5 8 

You can also sort an array of objects using a custom comparator. Here is an example of how to sort an array of strings in descending order based on their length:

import java.util.Arrays;
import java.util.Comparator;

public class SortExample {
    public static void main(String[] args) {
        String[] words = {"apple", "banana", "pear", "orange"};
        
        Arrays.sort(words, Comparator.comparingInt(String::length).reversed());
        
        for (String word : words) {
            System.out.print(word + " ");
        }
    }
}

Output:

banana orange apple pear 
int[] arr = {1, 2, 3, 4, 5};
int searchElement = 3;
boolean found = false;

for(int i=0; i<arr.length; i++) {
   if(arr[i] == searchElement) {
      found = true;
      break;
   }
}

if(found)
   System.out.println("Element found");
else
   System.out.println("Element not found");

In the above example, we have an integer array arr and a searchElement variable that stores the element we want to search. We use a for loop to iterate over the array and check if the current element is equal to the searchElement. If we find the element, we set the found variable to true and break out of the loop. After the loop, we check the found variable to see if the element was found.

For sorted arrays, you can use the Arrays.binarySearch() method which is more efficient than a simple for loop. Here is an example:

int[] arr = {1, 2, 3, 4, 5};
int searchElement = 3;

int index = Arrays.binarySearch(arr, searchElement);
if(index >= 0)
   System.out.println("Element found at index: " + index);
else
   System.out.println("Element not found");

In the above example, we use the Arrays.binarySearch() method to search for the element 3 in the arr array. The method returns the index of the element if it is found, otherwise it returns a negative value. We check if the index is greater than or equal to 0 to determine if the element was found. If it was found, we print the index, otherwise we print a message indicating that the element was not found.

      

Go through our study material. Your Job is awaiting.

Recent Posts
Categories