Lists and Structures. Peter Newman. Last week, we covered what single dimensional arrays and recursion were. . Quick Recap. Just to make sure... What is recursion, and what is a recursive construct known as? Give me the definition of an array ?
Last week, we covered what single dimensional arrays and recursion were.
This recursion were. week during lab practicals, we have to construct and populate 2D arrays. We will do this in more detail and discuss some other useful data structures you will use.
A single dimension array (1D) array is where we have a list of contiguous memory locations containing related data. A 2D array is where we have an array of arrays. The structure when drawn out resembles a table.
What does a 2D array look like? recursion were.
Again, best to illustrate with one of my famous Powerpoint diagrams.
Are they the same to use as normal arrays? recursion were.
int myIntArray = new int[ 10][ 10 ]; //almost the same, you give it col + //row
myIntArray = 10; //places an element in the ‘top left’ corner
myIntArray = 7; //places an element in the 1st row, 2nd column
myIntArray[...][...] = 9; //etc
System.out.println( myIntArray ); //prints out ‘10’
Last week you mentions something about arrays being used for Queues and Stacks...?
Yes! 2 VERY important data structures that use arrays (or similar mechanisms) for data storage).
Arrays are all I need! Queues and Stacks...
Arrays seem pretty darn awesome...is that all I need?
You wish! Arrays are limited in the way that they cannot be expanded. Once you set their size, that is the size they will be until they are ‘garbage-collected’. There are ways around this, but strictly speaking arrays don’t resize.
Well that sucks... Is there another way?
Yes there is... Through the power of linked-lists
Pause! Queues and Stacks...
Before I continue, there is something you need to know. This is were I illustrate on the whiteboard what ‘pass-by-reference’ and ‘pass-by-value are’
What is a linked list? Queues and Stacks...
A linked list is a solution to our limited size problem. They are a chain of objects that point to each other – forming a daisy chain that we can traverse. All that you need to monitor is the start element of the list.
Each element is a class that you will make that will store the data that you wish it to contain, and a variable that will hold a reference to the next element (if there is any).
The pages overleaf will show a working example.
Lets start with a list that is empty. As we will monitor the first element from our main program, we will just set it as null.
Lets add an element... Because Peter only did this 10 minutes before your tutorial, he will explain the intermediate steps...
Lets add another element... minutes before your tutorial, he will explain the intermediate steps...
Question Time! minutes before your tutorial, he will explain the intermediate steps...