0% found this document useful (0 votes)
8 views7 pages

6.1 pre calc

The document provides an overview of array creation and access in Java, explaining how to declare, create, and initialize arrays using both the 'new' keyword and initializer lists. It highlights the differences between primitive type arrays and object reference arrays, as well as how to access and modify array values. Additionally, it includes programming challenges and instructions for practicing array concepts on Runestone Academy.

Uploaded by

abdullah.ashban
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

6.1 pre calc

The document provides an overview of array creation and access in Java, explaining how to declare, create, and initialize arrays using both the 'new' keyword and initializer lists. It highlights the differences between primitive type arrays and object reference arrays, as well as how to access and modify array values. Additionally, it includes programming challenges and instructions for practicing array concepts on Runestone Academy.

Uploaded by

abdullah.ashban
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

6.1.

Array Creation and Access


• An array is a block of memory that stores a collection of data items (elements) of the
same type under one name.
• You can make arrays of ints, doubles, Strings, and even classes that you have written.

• You can store a value in an array using an index (location in the array).

6.1.1. Declaring and Creating an Array:


• To make a variable into an array, we put square brackets after the data type. For
example, int[] scores means we have an array called scores that contains int values.

• The declarations do not create an array. Arrays are objects in Java, so any variable that
declares an array holds a reference to an object.
• If the array hasn’t been created yet and you try to print the value of the variable, it will
print null (meaning it doesn’t reference any object yet).
• There are two ways to create an array. You can use the keyword new to get new memory or
use an initializer list to set up the values in the array.
1. Using the new Keyword
This method dynamically allocates memory for the array. You define the size of the array
and optionally initialize its elements later.

Syntax: declare and create array in 1 step!

declare and create array in 2 steps!

Example:

Key Points:
• Memory for the array is dynamically allocated.
• The size must be specified at the time of creation.
• All elements are initialized to default values (e.g., 0 for int, null for objects).

Now, go to Runestone Academy and complete all the activities in section 6.1.2.
2. Using an Initializer List
This method allows you to directly specify the array's contents using curly braces {}. The
size of the array is implicitly determined by the number of elements provided.
Syntax:

Example:

Key Points:
• No need to specify the size explicitly.
• Ideal when you know the initial values at the time of creation.
• The array is created and populated in a single step.

Comparing the Two Methods


Aspect Using new Keyword Using Initializer List
When to Use When you know the size but When you know the values
not the values. upfront.
Flexibility Allows dynamic initialization Static: all elements are set at
of elements. once.
Ease of Use More verbose. Concise and simpler.

Key Points to Understand


1. Primitive Type Array for example int [ ] highScores:
o What it does: Stores actual data values.

Example:

Explanation:
• Each element in the array stores a value of type int (a primitive type).
• The values (99, 98, etc.) are stored directly in contiguous memory locations.
• The indices (0, 1, 2, ...) are used to access these values.
Object Reference Array for example String[ ] names:
• What it does: Stores references (pointers) to objects, not the objects themselves.

Example:

Explanation:
• Each element in the array stores a reference (like an address) to a String object.
• The actual String objects (e.g., "Jamal", "Emily", etc.) are stored elsewhere in memory.
• The references in the array point to where these objects are located.
• This is because String is not a primitive type, it's an object type.
Why is "Jamal" a Reference in the Array?
In Java, a String is an object (not a primitive type like int or boolean), and objects are always
referenced in memory. This means:
• The names array itself does not store "Jamal"'s data directly (like "J", "a", "m", etc.).
• Instead, it stores a reference to the memory location where the "Jamal" object exists in
the String Literal Pool.
Think of the names array as a list of pointers:
• names[0] points to the "Jamal" object.
• names[1] points to the "Emily" object.
• And so on.
Final Note
So, "Jamal" is indeed a String. The names array doesn't directly store the "Jamal" data; it stores
a reference to the "Jamal" object in memory.
Simplified Analogy
• Primitive Arrays: Imagine a row of boxes, each containing a number directly (e.g., 99, 98,
68).
• Object Reference Arrays: Imagine a row of boxes, but instead of numbers, the boxes
contain pointers to people in a nearby room (e.g., Jamal, Emily, Sofia). The box doesn't
hold the person, just the address.
Task: Understanding Array Creation in Java
Instructions:
1. Part 1: Create an Array Using the new Keyword
o Create an array named grades that can store 5 integers.
o Initialize the array using the new keyword and assign the following values to it:
85, 90, 78, 92, 88.
o Print all the elements of the grades array using a loop.
2. Part 2: Create an Array Using an Initializer List
o Create an array named subjects that stores the names of 4 subjects: "Math",
"Science", "English", "History".
o Print all the elements of the subjects array using a loop.
3. Part 3: Analyze and Modify Arrays
o Modify the first element of the grades array to 95 and the last element of the
subjects array to "Art".
o Print the updated arrays to confirm the changes.

Example Output (Expected Result):

You can see this link


https://www.programiz.com/online-
compiler/4jRfg6TGqvJ9o
6.1.4. Array length
• How many elements the array stores.
• It is a public read-only instance variable so you can use dot-notation to access
the instance variable (arrayName.length)
Note:
• Note that length is an instance variable and not a method, unlike the String length() method,
so you don’t add parentheses after length. However, if you use parentheses after length
during the exam, you won’t lose any points.
• The length instance variable is declared as a public final int.
public means you can access it and
final means the value can’t change.
• length() is method .
• length without parentheses is property.

6.1.5. Access and Modify Array Values


• To access the items in an array, we use an indexed array variable
• indexed variable syntax: arrayname[index] = value ;
• The first value in an array is stored at index 0 and the index of the last value is the length of
the array minus one.
• Using an index value outside of 0 - (length-1) will result in an
ArrayIndexOutOfBoundsException being thrown.

Watch this video:


https://www.youtube.com/watch?v=uagEJw6bTM4&t=72s&ab_channel=colleenlewis

Now, go to Runestone Academy and complete all the activities in section 6.1.5
If you've tried enough and still can't figure out how to solve it, you can use ChatGPT—but only
after you've run out of attempts.
6.1.6. Programming Challenge: Countries’ Array

https://www.programiz.com/online-compiler/6kNv37wxDAAOp

6.1.7. Design an Array of Objects


• Notice that for an array of objects, we must call the constructor of each object to initialize the
array elements, for example array[index] = new ClassName();
And we can use array[index].method() to call a method of an object in the array.

Now, go to Runestone Academy and complete all the activities in section 6.1.7
If you've tried enough and still can't figure out how to solve it, you can use ChatGPT—but only
after you've run out of attempts.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy