Arrays in Java | Basics of Java - Software Development PDF Download

Introduction

Arrays are an essential part of programming as they allow us to store and manipulate collections of elements efficiently. In Java, an array is a fixed-size container that holds a homogeneous (same type) group of elements. In this article, we will explore the basics of arrays in Java, including their declaration, initialization, accessing elements, and performing common operations.

Declaring and Initializing Arrays

To declare an array in Java, we need to specify the data type of its elements followed by the array name and square brackets. Here's an example:

int[] numbers;

To initialize the array, we can use the new keyword followed by the data type and the desired size of the array:

numbers = new int[5];

Alternatively, we can combine declaration and initialization in a single line:

int[] numbers = new int[5];

We can also directly assign values to the array at the time of initialization:

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

Accessing Array Elements

Each element in an array is accessed using an index, starting from 0 for the first element. To access a specific element, we use the array name followed by the index enclosed in square brackets. Let's see an example:

int[] numbers = {10, 20, 30, 40, 50};

System.out.println(numbers[2]);

Output

30

In this example, we access the third element of the numbers array, which has an index of 2 (remember, indexes start from 0). The output will be 30.

Array Length

The length of an array represents the number of elements it can store. In Java, we can obtain the length of an array using the length property. Here's an example:

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

System.out.println(numbers.length);

Output

5

The output will be 5 since the numbers array contains five elements.

Modifying Array Elements

We can modify the values of array elements by assigning new values using the array name and the index. Here's an example:

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

numbers[2] = 10;

System.out.println(numbers[2]);

Output

10

Common Array Operations

Java provides several useful methods and operations to work with arrays. Here are some common ones:

  • Arrays.toString(): Converts an array into a string representation.

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

System.out.println(Arrays.toString(numbers));

Output

[1, 2, 3, 4, 5]

  • Arrays.sort(): Sorts the elements of an array in ascending order.

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

Arrays.sort(numbers);

System.out.println(Arrays.toString(numbers));

Output

[1, 2, 3, 4, 5]

  • Arrays.copyOf(): Creates a new array with a specified length and copies the elements from the original array.

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

int[] newNumbers = Arrays.copyOf(numbers, 3);

System.out.println(Arrays.toString(newNumbers));

Output

[1, 2, 3]

Sample Problems

1. Problem: Find the sum of all elements in an array.

int[] numbers = {10, 20, 30, 40, 50};

int sum = 0;

for (int i = 0; i < numbers.length; i++) {

    sum += numbers[i];

}

System.out.println("Sum: " + sum);

2. Problem: Find the maximum element in an array.

int[] numbers = {15, 8, 20, 5, 12};

int max = numbers[0];

for (int i = 1; i < numbers.length; i++) {

    if (numbers[i] > max) {

        max = numbers[i];

    }

}

System.out.println("Maximum: " + max);

3. Problem: Check if an array contains a specific element.

int[] numbers = {10, 20, 30, 40, 50};

int target = 30;

boolean contains = false;

for (int i = 0; i < numbers.length; i++) {

    if (numbers[i] == target) {

        contains = true;

        break;

    }

}

System.out.println("Contains " + target + ": " + contains);

Conclusion

Arrays are fundamental data structures in Java that allow us to store and manipulate collections of elements efficiently. In this article, we covered the basics of declaring, initializing, accessing, and modifying arrays. We also explored some common array operations. By understanding these concepts and practicing with sample problems, you'll be well on your way to mastering arrays in Java.

The document Arrays in Java | Basics of Java - Software Development is a part of the Software Development Course Basics of Java.
All you need of Software Development at this link: Software Development
60 videos|37 docs|12 tests

Up next

60 videos|37 docs|12 tests
Download as PDF

Up next

Explore Courses for Software Development exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

pdf

,

Semester Notes

,

Exam

,

mock tests for examination

,

Arrays in Java | Basics of Java - Software Development

,

Summary

,

Sample Paper

,

Viva Questions

,

video lectures

,

past year papers

,

Arrays in Java | Basics of Java - Software Development

,

Arrays in Java | Basics of Java - Software Development

,

study material

,

practice quizzes

,

Free

,

Important questions

,

Extra Questions

,

ppt

,

MCQs

,

Objective type Questions

,

shortcuts and tricks

,

Previous Year Questions with Solutions

;