22
Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be able to create one-dimensional and two-dimensional arrays. To be able to initialize one-dimensional and two-dimensional arrays To be able manipulate one-dimensional numeric arrays. To become familiar with arrays of objects including strings. To be able to apply the concept of arrays to searching and sorting.

Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be

Embed Size (px)

Citation preview

Page 1: Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be

Array

Objectives

• To understand the concept of arrays

• To understand the purpose to which we use arrays.

• To be able to declare references to arrays.

• To be able to create one-dimensional and two-dimensional arrays.

• To be able to initialize one-dimensional and two-dimensional arrays

• To be able manipulate one-dimensional numeric arrays.

• To become familiar with arrays of objects including strings.

• To be able to apply the concept of arrays to searching and sorting.

Page 2: Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be

Array

Introduction

Sometimes we have large sets of data that are needed at anytime in a program.

• If we use a single variable to store each value:

a) Each new data value would over write the previous one

b) At any point in time only one value could be accounted for

c) All the previous values would have been lost.

10 15 23

2035

7040

45 50 65

int x;

X = X = X = X = X =

X = X = X = X = X =

Using single variable to store data

Page 3: Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be

Array

Sometimes we have large sets of data that are needed at anytime in a program.

• We could use separate variable to represent each value:

a) Each new data value would be stored in its own variable

b) At any point in time we could accounted for each value

c) None of the previous values would ever be lost.

10 15 23

2035

7040

45 50 65

int x1, x2, x3, x4, x5, x6, x7, x8, x9, x10;

x1 = x2 = x3 = x4 = x5 =

x6 = x7 = x8 = x9 = x10 =

Using individual variable store data

Page 4: Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be

Array • Yet a third way would be:

a) Store the data contiguously in memory, and

b) Use one common name to refer to each location where data is stored.

• This method gives rise to the concept of array.

referenveVariable

The array

Page 5: Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be

Array

Array:

• A reserved set of contiguous storage locations to hold a fixed number of homogenous data elements.

• This means that:

1. All the elements in the array have the same data type

The type of values are – any primitive type or any object

2. The size of the array is fixed at compilation time.

3. The size of the array cannot be changed during run time.

Page 6: Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be

Array

10

15

23

40

70

35

20

45

50

65

int x[] The array

Array x references 10 (4 bytes) contiguously placed memory locations

Page 7: Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be

Declaring One-Dimensional Array Variables

• You must declare the array before you can use it. That is, you must specify:

The name of the variable that will reference the array, and The type of data that the array is expected to store.

• The format for declaring a one-dimensional array is as follows:data_type arr[];ordata_type [] arr;

• Where: data_type is any valid data type (primitive or reference type)

arr is the name of the array variable. The name follows the convention for name variables

[] is the array symbol. The order of placing the array symbol [] is commutative.

Page 8: Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be

Declaring One-Dimensional Array Variables

• int numbers[];

• String str[];

• byte[] someBytes;

• MotorCar sport[];

• char [] consonants;

• double x[];

• Circle [] c1;

Page 9: Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be

Declaring One-Dimensional Array Variables

1. // Declaration does not imply creating the array

2. class ArrayDeclaration

3. {

4. public static void main(String[] arg)

5. {

6. int numbers[];

7. System.out.println(numbers);

8. }

9. }

-------------------Configuration: j2sdk1.4.1_03 <Default>--------------------

C:\listing8.1\ArrayDeclaration.java:7: variable numbers might not

have been initialized System.out.println(numbers);

^ 1 error

Page 10: Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be

Creating One-Dimensional Array

• The format for creating a one-dimensional array is as follows:

• data_type []arr = new data_type [ numberOfCells ];

Or

• data_type arr [] = new data_type [ numberOfCells ];

• int x = new int[10];

• String str = new String[10];

• byte someBytes = new byte[10];

• Circle c1 = new Circle [10];

• char consonants = new char [10];

Page 11: Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be

Accessing The Cells Of The Array

0

1

2

3

4

5

6

7

8

9

indeces

referenceVariable • The cells in a linear (one -dimensional)

array are numbered by the operating

system.

• The numbering scheme begins with

the first cell labeled cell 0, the

second is labeled cell 1, the third is

labeled cell 2, and so on.

• These numbers are referred to as the

array indeces.

Page 12: Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be

Alternate Way to Create Array

• Java provides an alternate way to:

Declare

Construct, and

Explicitly initialize an array.

• The format is as follows:

data_type [] arrayName = {<individual values>};

• When using this construct the individual data must be separated by a comma

Page 13: Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be

Alternate Way to Create Array

• Create an array of the first 10 even integers beginning with the number two.

int evenNumbers[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};

• Create an array of all the vowels.

char [] vowels = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’};

• Create an array of the character escape sequence. Character escape sequence must also be placed within single quotation marks.

char escapes[] = {'\n', '\r', '\b', '\t', '\\', '\"', '\''};

• Create an array of menu-items for File, Edit, Insert, and Help.

String[] menu = {“File”, “Edit”, “Insert, “Help”};

Page 14: Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be

Manipulating a Linear Array

• You can assign values directly into the array.

• The general format of the assignment is as follows:

arr[i] = value;

Where:

• arr is the name of the array

• i is the array index, and

• value is the data value being assigned to cell.

• For instance, using the array evenNumbers:

evenNumbers [1] = 10;

• Or read value into array, as in:

evenNumbers [6] = GetData.getInt(“Enter an even integer”);

Page 15: Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be

Length of a Linear Array

Each array has an instance variable called length. It specifies the size of the array.

Givne that arra denotes an one-dimensional array, then the expression:

arr.length

retrieves the size of the array arr. The following piece of code finds the size of the array menu, and prints each value

in the array.

int size = menu.length;

for (int i = 0; i < size; i++)

System.out.println( menu[i] + “\n”);

Page 16: Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be

Convert Integer to Binary

Define a class called BinaryDigits that converts an integer value into its binary

equivalence.

Solution

• The class receives an integer value.

• Define a method that decomposes the number into binary digits.

• Return the binary digits.

Algorithm/rule for converting an integer into binary.

• Divide the original number by 2 and Record the remainder. Set the number to the quotient

• Repeat the process until the new number becomes zero.

• When the process stops, rewrite the result starting with the last value first.

Page 17: Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be

Convert Integer to Binary

• Let us use the number 53 to test this algorithm.

53/2 = 26 remainder 1

26/2 = 13 remainder 0

13/2 = 6 remainder 1

6/2 = 3 remainder 0

3/2 = 1 remainder 1

2/2 = 0 remainder 1

• Hence the binary equivalence of 53 is 1 1 0 1 0 1

Page 18: Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be

Convert Integer to Binary

Principal variables are:• The number• An array to store the remainder• An integer value to which to set the size of the array• An indexing variable for both the loop and the array.

Principal methods are:• The constructor• An instance method to implement the above algorithm• We may code the toString method to return the result

Page 19: Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be

Convert Integer to Binary

1. class BinaryDigits2. {3. int binary[], x, theNumber, index;4. static final int MAX = 32;5. BinaryDigits(int x)6. {7. binary = new int[MAX];8. this.x = x;9. index = 0;10. theNumber = x;11. }15. void makeBinaryDigits()16. {17. // …25. }26. public String toString()27. {28. // …….33. }34. }

Page 20: Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be

Convert Integer to Binary

15. void makeBinaryDigits()16. {17. int remainder;18. while (x > 0 && index < MAX)19. {20. remainder = x % 2;21. binary[index] = remainder;22. x = x/ 2;23. index = index + 1;24. }25. }

Page 21: Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be

Convert Integer to Binary

26. public String toString()

27. {

28. String s = "The number " + theNumber + " = ";

29. int index = this.index - 1;

30. for (int i = index; i >= 0; i-- )

31. s = s + binary[i] + " ";

32. return s + " in binary";

33. }

Page 22: Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be

Convert Integer to Binary

1. class TestBinary2. {3. public static void main(String[] arg)4. {5. BbinaryDigits b = new BinaryDigits(53);6. b.makeBinaryDigits();7. System.out.println(b);8. }9. }