Fiveable
Fiveable
pep
Fiveable
Fiveable

or

Log in

Find what you need to study


Light

Unit 6 Overview: Array

3 min readjanuary 8, 2023

Athena_Codes

Athena_Codes

Athena_Codes

Athena_Codes

The Big Takeaway Of This Unit

Unit Overview

Exam Weighting

  • 10-15% of the test
  • Roughly 4 to 6 multiple-choice questions
  • A possible topic of FRQ #3, which may test your ability to make and array algorithms.

Enduring Understanding

In this unit, we will start learning about data structures, which are structures that store multiple pieces of data. You will learn about three of them in this course: 1-D in this unit, in Unit 7, and in Unit 8.  store one type of data, whether that be a or a , and they are of . When used , we can do many things with and build algorithms with them, as well.

Building Computational Thinking

In this unit, you will learn three things that are important for larger programs: how to create an array, how to traverse(going through all the elements) an array, and how to manipulate the elements in an array. One of the common mistakes that you may make at first is an , which occurs when you try to access an element where none exists, but with some practice, you will be flawless with !

Main Ideas for this Unit

  • Initializing
  • Accessing Elements in
  • Array Algorithms

6.1: Array Creation and Access

Introduction to Arrays

 are used to store one type of data, whether it is a primitive or . themselves are reference types. They are best thought of as a list of items with a , as have a set size that cannot be changed (don’t confuse this with which can also be thought of as a list). are denoted by braces ({}), with items separated by commas such as the following:

{true, true, false, true}

Before we can use , we need to have an import statement, which is

import java.util.

Making Arrays

There are two ways to make : using a and using a pre-initialized array.

As with other reference types, we can initialize using a . However, the is slightly different from the constructors from Unit 5:

dataType[] arrayName = new dataType[numberOfItems];

The items in the array are initialized differently depending on the data type. Integers are initialized to 0, doubles are initialized to 0.0, booleans are initialized to false, and all reference types are initialized to null. We will talk about filling constructed lists in the next topic when we discuss .

Pre-initialized

We can also set an array to a pre-initialized array, similar to how we initialize strings. Here, we will initialize an array of 10 integers as follows:

int[] arrayOne = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

Accessing Elements in Arrays

We access elements in using bracket notation as follows: arrayName[index]. The most important thing to know is that Java is a , so the first item has index 0, and not 1.

Before we talk about the index of the last item, we need to discuss how to find the array length. The array length is actually an instance variable specific to that particular array denoted as  (not to be confused with length() for Strings). Note that this is not a method, so there are no parentheses. Thus, the last item in the array can be accessed by using  - 1. Do not confuse this with the in which we use in brackets. If we use an index outside the allowed range, we will get an .

Here is a question: how do we access the even numbers in arrayOne from above?

Answer

2 = arrayOne[1]

4 = arrayOne[3]

6 = arrayOne[5]

8 = arrayOne[7]

10 = arrayOne[9]

Key Terms to Review (13)

2-D Arrays

: 2-D Arrays (two-dimensional arrays) are rectangular grids or matrices consisting of rows and columns. They allow for storing and accessing elements using two indices - one for row number and another for column number.

ArrayIndexOutOfBoundsException

: ArrayIndexOutOfBoundsException is an exception that occurs when trying to access an invalid index position in an array. It is thrown to indicate that the index used to access an array is either negative or greater than or equal to the size of the array.

ArrayLists

: ArrayLists are dynamic arrays that can grow and shrink dynamically as needed. They provide resizable arrays with additional methods for easy manipulation and management.

arrayName.length

: The property "length" is used to determine the number of elements in an array.

arrayName[index]

: The syntax arrayName[index] is used in programming languages to access a specific element within an array. "arrayName" represents the name of the array, and "index" refers to the position of the desired element.

Arrays

: Arrays are a collection of elements of the same data type, stored in contiguous memory locations. They have a fixed size and can be accessed using an index.

Constructor

: A constructor is a special method within a class that is used to initialize objects of that class. It is called automatically when an object is created and helps set initial values for its attributes.

fixed size

: Fixed size refers to a data structure or container whose size cannot be changed once it is created. Once initialized with a specific capacity or length, no additional elements can be added or removed from it.

For Loops

: For loops are control structures used for repetitive execution of code blocks. They consist of an initialization statement, condition statement, increment/decrement statement, and loop body. The loop continues until the condition becomes false.

Primitive data type

: Primitive data types are basic data types provided by programming languages, such as integers, floating-point numbers, booleans, and characters. They represent simple values and have predefined characteristics.

Reference Data Type

: A reference data type is a data type that refers to an object in memory rather than directly storing the value. It stores the memory address where the object is located.

Traversing Arrays

: Traversing arrays refers to the process of accessing and examining each element in an array. It allows you to perform operations on every element or search for specific values within the array.

Zero-indexed language

: In computer programming, zero-indexed language refers to systems where counting starts from 0 instead of 1 when accessing elements in an ordered collection (e.g., arrays).

Unit 6 Overview: Array

3 min readjanuary 8, 2023

Athena_Codes

Athena_Codes

Athena_Codes

Athena_Codes

The Big Takeaway Of This Unit

Unit Overview

Exam Weighting

  • 10-15% of the test
  • Roughly 4 to 6 multiple-choice questions
  • A possible topic of FRQ #3, which may test your ability to make and array algorithms.

Enduring Understanding

In this unit, we will start learning about data structures, which are structures that store multiple pieces of data. You will learn about three of them in this course: 1-D in this unit, in Unit 7, and in Unit 8.  store one type of data, whether that be a or a , and they are of . When used , we can do many things with and build algorithms with them, as well.

Building Computational Thinking

In this unit, you will learn three things that are important for larger programs: how to create an array, how to traverse(going through all the elements) an array, and how to manipulate the elements in an array. One of the common mistakes that you may make at first is an , which occurs when you try to access an element where none exists, but with some practice, you will be flawless with !

Main Ideas for this Unit

  • Initializing
  • Accessing Elements in
  • Array Algorithms

6.1: Array Creation and Access

Introduction to Arrays

 are used to store one type of data, whether it is a primitive or . themselves are reference types. They are best thought of as a list of items with a , as have a set size that cannot be changed (don’t confuse this with which can also be thought of as a list). are denoted by braces ({}), with items separated by commas such as the following:

{true, true, false, true}

Before we can use , we need to have an import statement, which is

import java.util.

Making Arrays

There are two ways to make : using a and using a pre-initialized array.

As with other reference types, we can initialize using a . However, the is slightly different from the constructors from Unit 5:

dataType[] arrayName = new dataType[numberOfItems];

The items in the array are initialized differently depending on the data type. Integers are initialized to 0, doubles are initialized to 0.0, booleans are initialized to false, and all reference types are initialized to null. We will talk about filling constructed lists in the next topic when we discuss .

Pre-initialized

We can also set an array to a pre-initialized array, similar to how we initialize strings. Here, we will initialize an array of 10 integers as follows:

int[] arrayOne = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

Accessing Elements in Arrays

We access elements in using bracket notation as follows: arrayName[index]. The most important thing to know is that Java is a , so the first item has index 0, and not 1.

Before we talk about the index of the last item, we need to discuss how to find the array length. The array length is actually an instance variable specific to that particular array denoted as  (not to be confused with length() for Strings). Note that this is not a method, so there are no parentheses. Thus, the last item in the array can be accessed by using  - 1. Do not confuse this with the in which we use in brackets. If we use an index outside the allowed range, we will get an .

Here is a question: how do we access the even numbers in arrayOne from above?

Answer

2 = arrayOne[1]

4 = arrayOne[3]

6 = arrayOne[5]

8 = arrayOne[7]

10 = arrayOne[9]

Key Terms to Review (13)

2-D Arrays

: 2-D Arrays (two-dimensional arrays) are rectangular grids or matrices consisting of rows and columns. They allow for storing and accessing elements using two indices - one for row number and another for column number.

ArrayIndexOutOfBoundsException

: ArrayIndexOutOfBoundsException is an exception that occurs when trying to access an invalid index position in an array. It is thrown to indicate that the index used to access an array is either negative or greater than or equal to the size of the array.

ArrayLists

: ArrayLists are dynamic arrays that can grow and shrink dynamically as needed. They provide resizable arrays with additional methods for easy manipulation and management.

arrayName.length

: The property "length" is used to determine the number of elements in an array.

arrayName[index]

: The syntax arrayName[index] is used in programming languages to access a specific element within an array. "arrayName" represents the name of the array, and "index" refers to the position of the desired element.

Arrays

: Arrays are a collection of elements of the same data type, stored in contiguous memory locations. They have a fixed size and can be accessed using an index.

Constructor

: A constructor is a special method within a class that is used to initialize objects of that class. It is called automatically when an object is created and helps set initial values for its attributes.

fixed size

: Fixed size refers to a data structure or container whose size cannot be changed once it is created. Once initialized with a specific capacity or length, no additional elements can be added or removed from it.

For Loops

: For loops are control structures used for repetitive execution of code blocks. They consist of an initialization statement, condition statement, increment/decrement statement, and loop body. The loop continues until the condition becomes false.

Primitive data type

: Primitive data types are basic data types provided by programming languages, such as integers, floating-point numbers, booleans, and characters. They represent simple values and have predefined characteristics.

Reference Data Type

: A reference data type is a data type that refers to an object in memory rather than directly storing the value. It stores the memory address where the object is located.

Traversing Arrays

: Traversing arrays refers to the process of accessing and examining each element in an array. It allows you to perform operations on every element or search for specific values within the array.

Zero-indexed language

: In computer programming, zero-indexed language refers to systems where counting starts from 0 instead of 1 when accessing elements in an ordered collection (e.g., arrays).


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.