AP Computer Science in Java
  • Introduction
  • Introduction to Programming in Java with Karel the Dog
    • Introduction to Programming with Karel
    • More Basic Karel
    • Java Programs and the Run Method
    • Karel Can't Turn Right
    • Methods in Karel
    • Top Down Design and Decomposition in Karel
    • Commenting Your Code
    • SuperKarel
    • For Loops
    • While Loops in Karel
    • If Statements
    • If/Else Statements
    • Control Structures Example
    • How To Indent Your Code
  • Basic Java
    • Printing in Java
    • Variables and Types
    • User Input
    • Arithmetic Expressions
    • Casting
    • Booleans
    • Logical Operators
    • Comparison Operators
    • For Loops
    • While Loops
    • If Statements
    • Loop-and-a-Half
    • Short-Circuit Evaluation
    • De Morgan's Laws
    • Strings
  • Methods
    • Java Methods
    • Methods and Parameters
    • Methods and Return Values
    • Javadoc and More Methods
    • Strings Methods
    • Strings and Characters
    • Exceptions
    • String Processing
  • Classes and Object-Oriented Programming
    • Introduction To Classes and Objects
    • Classes vs. Objects
    • Using a Class as a Client
    • Writing Classes
    • Writing Classes and Instance Methods
    • Getter and Setter Methods
    • Class Methods and Class Variables
    • Method Overloading
    • Local Variables and Scope
    • Key Terms for Classes
    • Objects vs Primitives
    • Inheritance
    • Class Design and Abstract Classes
    • Polymorphism
    • Interfaces
  • Data Structures
    • What Are Data Structures?
    • Introduction to Arrays
    • Using Arrays
    • ArrayList Methods
    • Arrays vs ArrayLists
    • 2D Arrays (Matrices or Grids)
    • Hashmaps
  • Algorithms and Recursion
    • What is an Algorithm?
    • Pseudocode
    • Linear Search
    • Binary Search
    • Selection Sort
    • Insertion Sort
    • Advanced: Recursion
    • Mergesort
Powered by GitBook
On this page
  • Accessing the Array
  • Accessing a Single Element:
  • Iterating Over The Array

Was this helpful?

  1. Data Structures

Using Arrays

Arrays are utilized for many operations in various programs. Since arrays store data in order, we can iterate through them to access this data. Let's say we are creating an online store that sells oranges. We would want to store the customer orders in an array so we can honor the first orders before the orders that come later.

Accessing the Array

There are many ways we can access the items within our array.

Let's say we have an array that stores the order data from our market. Here is the array:

Index:

0

1

2

3

4

5

Orders:

10 Oranges

3 Oranges

6 Oranges

4 Oranges

5 Oranges

1 Orange

In Java our array will look like:

int[] orangeOrders = {10, 3, 6, 4, 5, 1};

Accessing a Single Element:

Our third order index 2 is for 6 oranges, but what if the customer made a mistake and only wants 3 oranges?

In this case we would access the array, and change the value at index 2 to 6. To do this we would use:

orangeOrders[2] = 3;

The value at index 2 in our array is set to 3.

Now, let's say orders at index 4 and index 5 paid for an express purchase. We need to push these two items into our order queue before the rest of our orders. To do this we would use orangeOrders[4] and orangeOrders[5], and push them through our queue using finalizeOrder(order);.

Our code will look like:

finalizeOrder(orangeOrders[4]);
finalizeOrder(orangeOrders[5]);

Iterating Over The Array

It is the end of the day and our store has stopped accepting new orders. Now we need to send the current array of orders to be finalized. We don't want to access each item in the array one-by-one and rewrite code. So we will need to iterate over our array using a for loop. Since we are using a for loop, how do we know how many iterations it should perform? We use arr.length to see how many items are in our array.

Our code will look something like:

int[] orangeOrders = {10, 3, 6, 4, 5, 1};

for(int i = 0; i < orangeOrders.length; i++)
{
    finalizeOrder(orangeOrders[i]);
}

We can also iterate over our array to determine how many oranges have been sold using:

int[] orangeOrders = {10, 3, 6, 4, 5, 1};
int sumOfOranges = 0;

for(var i = 0; i < orangeOrders.length; i++)
{
  sumOfOranges += orangeOrders[i];
}

System.out.println("Total oranges sold: " + sumOfOranges);
PreviousIntroduction to ArraysNextArrayList Methods

Last updated 5 years ago

Was this helpful?