Você está na página 1de 4

Arrays

Programs often use large quantities of similar data


Assigning a unique variable (and name) to each piece of
data is tedious

Arrays in Java
CSE 110: Introduction to
Computer Science

Ex. var1, var2, var3, ...


An array is a collection of many variables of the same type,
all under one name
Arrays can be of any type

Stony Brook University

Array Storage
int[] a = new int[6];

Declaring An Array
To declare an array, follow the type with square brackets:
double[] foo;

a[0] a[1] a[2] a[3] a[4] a[5]

To create an array, use new and supply a size:


foo = new double[5];
If this looks like what you do for a class, youre right;
arrays are actually (specialized) objects
The array size must be a positive integer

Array Size

Examples
Definitions:

The size of an array is fixed upon creation


The length field returns an arrays size
int[] a = new int[5];
int size = a.length; // size is 5
Note that length has no parentheses!
Its a field (variable), not a function/method

char[] c;
int [] value = new int[10];
End Result:
Array object variable c is un-initialized
Array object variable v references a new ten-element list
of integers
Each of the integers is initialized to 0 by default

Array Examples
char[] alphabet = new char[26];

Creating Arrays
Arrays can be initialized at declaration:
int [] bar = {5, 4, 3, 2, 1};

int numPlayers = 5;
int[] scores = new int[numPlayers];

The number of items in the initialization list sets the size of


the array
In this example, bar has five elements
The array is filled with the values in the initialization list

String[] phrases = new String[15];

Arrays and Loops

Array Elements

Loops (especially for loops) are the perfect way to


manipulate arrays:

Individual elements of an array are accessed by using the


array name, followed by an (integer) index value, enclosed
in brackets
Ex. myArray[1]
Indices are numbered starting with 0
e.g., myArray[1] refers to the second element in
myArray

int [] a = new int[5];


for (int i = 0; i < a.length; i = i + 1)
{
!

a[i] = i * 2;

Fibonacci Numbers

Special sequence of integers


First and second numbers are 0 and 1, respectively
Each subsequent number is the sum of the two previous
Fibonacci numbers
Ex. Fib(2) = Fib(1) + Fib(0)

int range = sc.nextInt();


int [] fib = new int[range];
fib[0] = 0;
fib[1] = 1;
for (int i = 2; i < range; i++)
{
fib[i] = fib[i-1] + fib[i-2];
}
// fib[n] contains the nth Fibonacci number

A Sample Array Problem


Write a method that examines two integer arrays representing
lottery numbers (one contains the winning numbers, and one
contains a given players selected numbers). The method
returns the number of values that appear in both arrays (i.e.,
the intersection of the two arrays). You may assume that the
arrays are of equal length and that each array only contains one
copy of a single number.
Ex. {7, 4, 9, 1, 3} and {4, 2, 9, 7, 3} have four values in common
(3, 4, 7, and 9)

Sample Lottery Solution


// Assume that source arrays are called first and second
int matches = 0;
// Compare each element of first to each value in second
for (int i = 0; i < first.length; i = i + 1)
{
for (int k = 0; k < second.length; k = k + 1)
{
if (first[i] == second[k])
// we found a match
{ matches = matches + 1; }
}
}

Você também pode gostar