Transcript Arrays PPT
Improved Array Definition
An array is a data structure with a fixed number of
elements of the same type.
Every element of the array can be accessed directly.
Array Example
[16]
Ingrid
[17]
Darlene
[18]
Gene
[19]
Sean
[20]
Stephanie
[11]
Holly
[12]
Blake
[13]
Michelle
[14]
Remy
[15]
Haley
[06]
Diana
[07]
Jessica
[08]
David
[09]
Anthony
[10]
Alec
[01]
Isolde
[02]
John
[03]
Greg
[04]
Maria
[05]
Heidi
Declaring a 1D Array
int list[ ];
list = new int[10];
// declares the array list identifier
// allocates memory for 10 integers
char names[ ];
names = new char[25];
// declares the names array identifier
// allocates memory for 25 characters
double grades[ ];
// declares the grades array identifier
grades = new double[50];
// allocates memory for 50 doubles
// Java1301.java
// This program demonstrates how to declare an array of integers.
Java1301.java Output
// Note how each element of the array defaults to zero.
// Java allows the display of uninitialized objects because object
List[0] = 0
// elements get assigned values from the default constructor.
List[1] = 0
List[2] = 0
List[3] = 0
public class Java1301
List[4] = 0
{
List[5] = 0
public static void main(String args[]) List[6] = 0
{
List[7] = 0
int list[];
// declares the array
object
List[8]
= 0 identifier
list = new int[10]; // allocates memory
for =100 array elements
List[9]
for (int k = 0; k < 10; k++)
System.out.println("list[" + k + "] = " + list[k]);
System.out.println();
}
}
Array Index Note
Java arrays indicate individual elements with an
index inside two brackets, following the array
identifier, like list[k].
The array index is always an integer and starts at 0.
In an array of N elements, the largest index is N-1.
// Java1302.java
// This program is almost identical to Java1301.
The difference
Java1302.java
Output
// is that the array declaration and array definition to allocate
// memory is done in one program statement. List[0] = 0
List[1] = 0
List[2] = 0
List[3] = 0
public class Java1302
List[4] = 0
{
public static void main(String args[]) List[5] = 0
List[6] = 0
{
List[7] = 0
int list[] = new int[10];
List[8] = 0
// combined array declaration and definition
List[9] = 0
for (int k = 0; k < 10; k++)
System.out.println("list[" + k + "] = " + list[k]);
System.out.println();
}
}
Java1303.java Output
// Java1303.java
// This program demonstrates how to initialize array elements.
List[0] = 11
// The <new> operator is not necessary in this case.
List[1] = 22
List[2] = 33
List[3] = 44
List[4] = 55
public class Java1303
List[5] = 66
{
List[6] = 77
public static void main(String args[])
List[7] = 88
{
List[8] = 99
int list[] = {11,22,33,44,55,66,77,88,99};
for (int k = 0; k < 9; k++)
System.out.println("list[" + k + "] = " + list[k]);
System.out.println();
}
}
// Java1304.java
Java1304.java Output
// This program demonstrates how to declare an array of characters.
// Are the array elements initialized to any kindList1[0]
of value?=
List1[1] =
List1[2] =
public class Java1304
List1[3] =
{
List1[4] =
public static void main(String args[])
List2[0] = c
{
List2[1] = h
int k;
List2[2] = a
char list1[] = new char[5];
List2[3] = r
for (k = 0; k < 5; k++)
System.out.println("list1[" + k + "] = " + list1[k]);
System.out.println();
char list2[] = {'c','h','a','r'};
for (k = 0; k < 4; k++)
System.out.println("list2[" + k + "] = " + list2[k]);
System.out.println();
}
}
// Java1305.java
// The purpose of this program is to investigate the type of character that
// is used to initialize a character array.
// Is it no-character-at-all or a blank space?
public class Java1305
{
public static void main(String args[])
{
Java1305.java Output
char List1[] = new char[10];
System.out.print("Start");
Start
End
for (int K = 0; K < 10; K++)
System.out.print(List1[K]);
System.out.println("End");
System.out.println();
}
}
// Java1306.java
Java1306.java Output
// This program demonstrates how String objects are initialized,
// both without and with specified array values.List[0] = null
List[1] = null
List[2] = null
public class Java1306
List[3] = null
{
List[4] = null
public static void main(String args[])
List2[0] = AAA
{
List2[1] = BBB
String list1[] = new String[5];
List2[2] = CCC
for (int k = 0; k < 5; k++)
List2[3] = DDD
System.out.println("list[" + k + "]List2[4]
= " + list1[k]);
= EEE
System.out.println();
String list2[] = {"AAA","BBB","CCC","DDD","EEE"};
for (int k = 0; k < 5; k++)
System.out.println("list2[" + k + "] = " + list2[k]);
System.out.println();
}
}
Java1307.java Output
// Java1307.java
// This program fills an integer array with a random setList[0]
of numbers.
= 851
import java.util.Random;
public class Java1307
{
public static void main(String args[])
{
int list[] = new int[20];
Random random = new Random(12345);
for (int k = 0; k < 20; k++)
list[k] = random.nextInt(900) + 100;
for (int k = 0; k < 20; k++)
System.out.println("list[" + k + "] = " +
System.out.println();
}
}
List[1] = 680
List[2] = 241
List[3] = 928
List[4] = 458
List[5] = 284
List[6] = 575
List[7] = 802
List[8] = 701
List[9] = 889
List[10] = 717
List[11] = 142
List[12] = 890
List[13] = 206
List[14] = 312
List[15] = 584
List[16] = 687
List[17] = 803
list[k]);
List[18] = 432
List[19] = 775
// Java1308.java
Java1308.java Output
// This program introduces the length field to determine the
There are
4 array elements.
// number of elements in the array. Remove
the comments
from line 16
// to observe what happens when the Names[0]
length field =
is Joe
altered.
Names[1] = Tom
public class Java1308
Names[2] = Sue
{
Names[3] = Meg
public static void main(String args[])
{
String names[] = {"Joe","Tom","Sue","Meg"};
int n = names.length;
// data field access; not a method call
System.out.println("There are " + n + " array elements.");
for(int k = 0; k < n; k++)
System.out.println("names[" + k + "] = " + names[k]);
// names.length = 10;
System.out.println();
}
}
Java1309.java Output
// Java1309.java
// This program demonstrates how to create an array of random strings.
Names[9] = JJ
Names[7] = HH
import java.util.Random;
Names[9] = JJ
Names[8] = II
public class Java1309
Names[8] = II
{
Names[1] = BB
Names[6] = GG
public static void main(String args[])
Names[0] = AA
{
Names[1] = BB
Random random = new Random(12345);
Names[0] = AA
int rndInt;
Names[7] = HH
String names[] = {"AA","BB","CC","DD","EE","FF","GG","HH","II","JJ"};
Names[8] = II
for(int k = 1; k < 15; k++)
Names[0] = AA
{
Names[3] = DD
rndInt = random.nextInt(names.length);
System.out.println("names[" + rndInt + "] = " + names[rndInt]);
}
System.out.println();
}
}