0% found this document useful (0 votes)
32 views10 pages

Data Structure #2: Arrays

Arrays are a commonly used data structure that allow storing a collection of data of the same type linearly in memory. Key operations on arrays include insertion, searching, and deletion. In Java, arrays are considered references - when an array is declared, it contains a reference to the memory location of the array rather than the actual elements. Individual elements in a Java array are accessed via an index number in square brackets.

Uploaded by

Muhammad Talha
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)
32 views10 pages

Data Structure #2: Arrays

Arrays are a commonly used data structure that allow storing a collection of data of the same type linearly in memory. Key operations on arrays include insertion, searching, and deletion. In Java, arrays are considered references - when an array is declared, it contains a reference to the memory location of the array rather than the actual elements. Individual elements in a Java array are accessed via an index number in square brackets.

Uploaded by

Muhammad Talha
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/ 10

Data Structure #2:

Arrays
CS214, Data Structures
The Array
Most commonly used data structure
Common operations
Insertion
Searching
Deletion
How do these differ for an ordered array?
How do these differ for an array which does not allow
duplicates?
Array Storage
An array is a collection of data of the same type
Stored linearly in memory:
Remember, value vs. reference
In Java:
Data of a primitive type is a ____________.
All objects are ________________.

Java arrays are also considered references.


Defining a Java Array
Say, of 100 integers:
int[] intArray;
intArray = new int[100];

We can combine these statements:

Or, change the [] to after the variable name

What do the [] signify?


We said an array was a reference
That means if we do this:
int[] intArray;
intArray = new int[100];

What exactly does intArray contain? Lets look internally.


The Size
Size of an array cannot change once its been declared:
intArray = new int[100];

But, one nice thing is that arrays are objects. So you can access
its size easily:
int arrayLength = intArray.length;

Getting an array size is difficult in many other languages


Access
Done by using an index number in square brackets:
int temp = intArray[3]; // Gets 4th element
intArray[7] = 66; // Sets 8th element

How do we access the last element of the array, if we dont


remember its size?

What range of indices will generate the IndexOutOfBounds


exception?

The index is an offset. Lets look at why.


Initialization
What do the elements of this array contain:
int[] intArray = new int[100];
How about this one:
BankAccount[] myAccounts = new BankAccount[100];
What happens if we attempt to access one of these values?

int[] intArray = {0, 3, 6, 9, 12, 15, 18, 21, 24,


27};

Automatically determines the size


Can do this with primitives or objects
Look at a book example
See the example on p. 41-42, where we do the following:
Insert 10 elements into an array of integers
Display them
Find item with key 66
Delete item with key 55
Display them

Ask ourselves:
How could we make the initialization shorter?
How could we save declaring nElems?

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