Subjects

Knowunity AI

Creator Program

Open the App

Subjects

AP Computer Science AAP Computer Science A84 views·Updated Aug 26, 2026·2 pages

How to Use Arrays in Java: Simple Examples & Fun Tricks

Arrays in Java: Declaration, Usage, and Common Algorithms

Arrays are...

1
of 2
Unit 6 - Arrays (Java) – page 1

Advanced Array Operations in Java

This section covers more complex array operations, including element removal, insertion, swapping, and array growth.

Removing an Element

  1. Unordered removal:

    • Replace the element to be removed with the last element of the array.
  2. Ordered removal:

    • Shift all elements after the removed element up by one index.
    • Use a placeholder variable and decrement the size variable.

Example: To remove nums[2] in an ordered array, move nums[3] to nums[2], nums[4] to nums[3], and so on.

Inserting an Element

  1. Check if there's room in the array.
  2. Move all elements after the intended insertion spot one index higher.
  3. Insert the new element at the desired location.

Highlight: When inserting an element, always ensure there's enough space in the array to avoid ArrayIndexOutOfBoundsException.

Swapping Elements

Use a temporary variable to swap elements using their indices:

int temp = array[i];
array[i] = array[j];
array[j] = temp;

Growing an Array

Since arrays in Java have a fixed size, growing an array requires creating a new, larger array:

  1. Create a new array with the desired larger size.
  2. Loop through the original array, copying all values to the new array.
  3. Assign the new array to the original array variable.

Example:

int[] oldArray = {1, 2, 3};
int[] newArray = new int[oldArray.length * 2];
for (int i = 0; i < oldArray.length; i++) {
    newArray[i] = oldArray[i];
}
oldArray = newArray;

Vocabulary: Array growth - The process of creating a new, larger array and copying elements from the old array to accommodate more elements.

These advanced operations are crucial for efficient array manipulation in Java, enabling developers to perform complex data management tasks within their applications.

2
of 2
Unit 6 - Arrays (Java) – page 2

Arrays in Java: Declaration and Initialization

Arrays in Java are objects that store multiple values of the same data type. They offer efficient ways to handle collections of data.

Definition: An array is a fixed-size, ordered collection of elements of the same data type.

Declaring and Instantiating Arrays

There are multiple ways to declare and instantiate arrays in Java:

  1. Declare and initialize in one line:

    datatype[] name = {x, y, z};
    

    Example: int[] nums = {2, 4, 6};

  2. Declare with a specific length:

    datatype[] name = new datatype[length];
    

    Example: int[] nums = new int[5];

Highlight: Arrays in Java are objects, not primitive types.

Array Properties

  • Common array types include int[], double[], and String[].
  • Arrays can also store custom objects, e.g., Car[] cars = {car1, car2};
  • Array indices start at 0 and end at length - 1.
  • To get the length of an array, use arrayName.length.
  • You can modify elements in an array, but not its length after initialization.
  • Arrays can be used as parameter values and return values in methods.
  • Arrays can be partially filled.

Vocabulary: Object reference - When you set one array equal to another, it creates a reference to the same object in memory.

Common Algorithms for Array Manipulation

  1. Target Search:

    • Use a loop and conditional statement.
    • Check if nums[x] is the target and save the index.
  2. Accumulation:

    • Initialize a sum variable.
    • Loop through the array, incrementing the index and adding nums[x] to the sum.
  3. Finding Maximum/Minimum:

    • Initialize a max/min variable with the first element of the array.
    • Loop through, comparing each element to the current max/min and updating if necessary.
  4. Checking for Increasing or Decreasing Order:

    • Use a loop and conditional statement.
    • Ensure the loop length only goes up to length - 1 to avoid Out Of Bounds errors.
    • Compare nums[x] with nums[x+1] to check for increasing or decreasing order.

We thought you’d never ask...

Our AI companion is specifically built for the needs of students. Based on the millions of content pieces we have on the platform we can provide truly meaningful and relevant answers to students. But its not only about answers, the companion is even more about guiding students through their daily learning challenges, with personalised study plans, quizzes or content pieces in the chat and 100% personalisation based on the students skills and developments.

You can download the app in the Google Play Store and in the Apple App Store.

That's right! Enjoy free access to study content, connect with fellow students, and get instant help – all at your fingertips.

Most popular content in AP Computer Science A

4

Most popular content

9

Students love us — and so will you.

4.6/5App Store
4.7/5Google Play

The app is very easy to use and well designed. I have found everything I was looking for so far and have been able to learn a lot from the presentations! I will definitely use the app for a class assignment! And of course it also helps a lot as an inspiration.

Stefan SiOS user

This app is really great. There are so many study notes and help [...]. My problem subject is French, for example, and the app has so many options for help. Thanks to this app, I have improved my French. I would recommend it to anyone.

Samantha KlichAndroid user

Wow, I am really amazed. I just tried the app because I've seen it advertised many times and was absolutely stunned. This app is THE HELP you want for school and above all, it offers so many things, such as workouts and fact sheets, which have been VERY helpful to me personally.

AnnaiOS user

AP Computer Science AAP Computer Science A84 views·Updated Aug 26, 2026·2 pages

How to Use Arrays in Java: Simple Examples & Fun Tricks

Arrays in Java: Declaration, Usage, and Common Algorithms

Arrays are fundamental data structures in Java, used to store multiple elements of the same type. This guide covers array declaration, initialization, and common algorithms for array manipulation.

Bold keywords: Array...

1
of 2
Unit 6 - Arrays (Java) – page 1

Sign up to see the content. It's free!

  • Access to all documents
  • Improve your grades
  • Join milions of students

Advanced Array Operations in Java

This section covers more complex array operations, including element removal, insertion, swapping, and array growth.

Removing an Element

  1. Unordered removal:

    • Replace the element to be removed with the last element of the array.
  2. Ordered removal:

    • Shift all elements after the removed element up by one index.
    • Use a placeholder variable and decrement the size variable.

Example: To remove nums[2] in an ordered array, move nums[3] to nums[2], nums[4] to nums[3], and so on.

Inserting an Element

  1. Check if there's room in the array.
  2. Move all elements after the intended insertion spot one index higher.
  3. Insert the new element at the desired location.

Highlight: When inserting an element, always ensure there's enough space in the array to avoid ArrayIndexOutOfBoundsException.

Swapping Elements

Use a temporary variable to swap elements using their indices:

int temp = array[i];
array[i] = array[j];
array[j] = temp;

Growing an Array

Since arrays in Java have a fixed size, growing an array requires creating a new, larger array:

  1. Create a new array with the desired larger size.
  2. Loop through the original array, copying all values to the new array.
  3. Assign the new array to the original array variable.

Example:

int[] oldArray = {1, 2, 3};
int[] newArray = new int[oldArray.length * 2];
for (int i = 0; i < oldArray.length; i++) {
    newArray[i] = oldArray[i];
}
oldArray = newArray;

Vocabulary: Array growth - The process of creating a new, larger array and copying elements from the old array to accommodate more elements.

These advanced operations are crucial for efficient array manipulation in Java, enabling developers to perform complex data management tasks within their applications.

2
of 2
Unit 6 - Arrays (Java) – page 2

Sign up to see the content. It's free!

  • Access to all documents
  • Improve your grades
  • Join milions of students

Arrays in Java: Declaration and Initialization

Arrays in Java are objects that store multiple values of the same data type. They offer efficient ways to handle collections of data.

Definition: An array is a fixed-size, ordered collection of elements of the same data type.

Declaring and Instantiating Arrays

There are multiple ways to declare and instantiate arrays in Java:

  1. Declare and initialize in one line:

    datatype[] name = {x, y, z};
    

    Example: int[] nums = {2, 4, 6};

  2. Declare with a specific length:

    datatype[] name = new datatype[length];
    

    Example: int[] nums = new int[5];

Highlight: Arrays in Java are objects, not primitive types.

Array Properties

  • Common array types include int[], double[], and String[].
  • Arrays can also store custom objects, e.g., Car[] cars = {car1, car2};
  • Array indices start at 0 and end at length - 1.
  • To get the length of an array, use arrayName.length.
  • You can modify elements in an array, but not its length after initialization.
  • Arrays can be used as parameter values and return values in methods.
  • Arrays can be partially filled.

Vocabulary: Object reference - When you set one array equal to another, it creates a reference to the same object in memory.

Common Algorithms for Array Manipulation

  1. Target Search:

    • Use a loop and conditional statement.
    • Check if nums[x] is the target and save the index.
  2. Accumulation:

    • Initialize a sum variable.
    • Loop through the array, incrementing the index and adding nums[x] to the sum.
  3. Finding Maximum/Minimum:

    • Initialize a max/min variable with the first element of the array.
    • Loop through, comparing each element to the current max/min and updating if necessary.
  4. Checking for Increasing or Decreasing Order:

    • Use a loop and conditional statement.
    • Ensure the loop length only goes up to length - 1 to avoid Out Of Bounds errors.
    • Compare nums[x] with nums[x+1] to check for increasing or decreasing order.

We thought you’d never ask...

Our AI companion is specifically built for the needs of students. Based on the millions of content pieces we have on the platform we can provide truly meaningful and relevant answers to students. But its not only about answers, the companion is even more about guiding students through their daily learning challenges, with personalised study plans, quizzes or content pieces in the chat and 100% personalisation based on the students skills and developments.

You can download the app in the Google Play Store and in the Apple App Store.

That's right! Enjoy free access to study content, connect with fellow students, and get instant help – all at your fingertips.

Most popular content in AP Computer Science A

4

Most popular content

9

Students love us — and so will you.

4.6/5App Store
4.7/5Google Play

The app is very easy to use and well designed. I have found everything I was looking for so far and have been able to learn a lot from the presentations! I will definitely use the app for a class assignment! And of course it also helps a lot as an inspiration.

Stefan SiOS user

This app is really great. There are so many study notes and help [...]. My problem subject is French, for example, and the app has so many options for help. Thanks to this app, I have improved my French. I would recommend it to anyone.

Samantha KlichAndroid user

Wow, I am really amazed. I just tried the app because I've seen it advertised many times and was absolutely stunned. This app is THE HELP you want for school and above all, it offers so many things, such as workouts and fact sheets, which have been VERY helpful to me personally.

AnnaiOS user