Download presentation
Presentation is loading. Please wait.
1
Arrays
2
Java provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
3
Instead of declaring individual variables, such as number0, number1,
Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index.
4
Using array made of char produce ‘Hello’
5
Create an Array of four characters
6
Two input array String and int
7
Loop within the Array
8
Array sort numbers using index method
9
Create an Array to assign elements
10
Fill Array with a number
11
Range
12
2D Array a multi-dimensional data structure, that is, a multi-dimensional array. A two- dimensional array is really nothing more than an array of arrays (a three-dimensional array is an array of arrays of arrays)
13
2D Array your dinner. You could have a one-dimensional list of everything you eat: (lettuce, tomatoes, steak, mashed potatoes, cake, ice cream) Or you could have a two-dimensional list of three courses, each containing two things you eat: (lettuce, tomatoes) and (steak, mashed potatoes) and (cake, ice cream)
14
And a two-dimensional array looks like this:
2D array int[] myArray = {0,1,2,3}; And a two-dimensional array looks like this: int[][] myArray = { {0,1,2,3}, {3,2,1,0}, {3,5,6,1}, {3,8,3,4} };
18
2D array
19
2D array
20
2D array
25
3D array
28
Jagged Array
29
Data Structure A record organises the data by an attribute such as name, address, post, code, number etc Data stored in a list is stored in a linear fashion and stacks and queues are implementations of this data structure using specific methods for inserting and removing data
30
Data is added to and removed from the top of the list
Stacks: is one method for handling linear lists of data. The data placed one on the top of other 39 (top) 23 45 17 (bottom) 77 (top) 39 23 45 17 (bottom) Data is added to and removed from the top of the list
31
Pushing Poping 11 push Pop x 2 39 45 17 39 45 17 45 17
32
If the stack becomes full or empty, error message must be generated and a rogue value for the stack pointer such as -1
33
Pseudo Code If stack pointer max then report stack full Else
Set the stack pointer to stack pointer + 1 Set stack(stack pointer) to data Endif If stack pointer minimum then report stack empty Else Set data to stack (stack pointer) Set stack pointer to stack pointer -1 Endif
34
If stack pointer max then report stack full else set the stack pointer to stack pointer + 1 set stack(stack pointer) to date end if
35
If stack pointer minimum then report stack empty else set data to stack(stack pointer) set stack pointer to stack pointer -1 Endif
36
Proccedure add_to_queu (item) If rear == max then Queu_full=true Else
Front=front+1 Queue[front]=item Endif endprocedure rear front
37
A Stack contains the values 3,4,5 with 3 being the first value stored and 5 the last. Show how the stack changes when the following sequence of commands is used: POP PUSH7 POP PUSH8 PUSH9 3 4 5
38
Queues: A queue is a FIFO structure
Queues: A queue is a FIFO structure. The data is placed into a queue at the end of the queue and removed from the front of the queue. The data does not actually move forward in the queue but two pointers, start and end, track the data items in the structure.
39
39 45 17 Start pointer1 end pointer 3 45 17 After pop Start pointer2 end pointer 3
40
45 17 11 After push Start pointer2 end pointer 4 45 17 11 23 After push Start pointer2 end pointer 5
41
A Queue contains the values 3,4,5 with 3 being the first value stored and 5 the last. Show how the stack changes when the following sequence of commands is used: POP PUSH7 POP PUSH8 PUSH9 3 4 5
42
Circular queue: when two more data items pushed into the queue and the queue is full the item will be added in location 1. once full if any attempts to add more when is full will result to error message 62 45 17 11 23 57 After push end pointer 6 Start pointer2
43
If the start pointer = 1 and the end pointer = max then report that the queue is full elseif the start pointer = the endpointer + 1 report that the queue is full else add data at end pointer + 1 set end pointer to end pointer + 1 endif
44
If start pointer = 0 then report queue empty else data = queue(start pointer) set start pointer to start pointer + 1 endif
45
Write a program to store a value input by the user into two-dimensional array.
Int [][] values = new int[Rows][Columns]; for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) array[i][j] = input.nextInt();
46
Write a program to store a value input by the user into 5 by 5 two-dimensional array.
Int [][] values = new int[5][5];
48
Data Type Identifier Assigned Initial Value
49
Array called board: Dimension? Identifier? Data type?
Board(1,1) contains the letter in the top left corner of the display board Board(3,15) contains the letter in the bottom right corner of the display board
50
Example PROCEDURE ClearDisplay FOR Row = 1 TO 3 FOR Column = 1 to …..
Board (Row, ……) NEXT Column NEXT …… END PROCEDURE
51
FOR I = I to 20 WHILE x< 10 P = …. Q = Q + …… NEXT ENDWHILE
52
For Row = 1 TO 3 FOR Column = 1 TO 9 IF Ticket(Row, …..) = 0 THEN PRINT ‘ ‘ ELSE PRINT Ticket(….., Column) END IF NEXT Column Go to new line NEXT ROW
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.