05 - Data Structures: Lists
Goals
- Understand the concept of lists in programming
- Make exercises with Java implementation of lists
Slides
Exercises
Exercise 1
Build a train using ArrayList
and LinkedList
. Create an ArrayList
that holds the waggons, then create a LinkedList
that contains the waggons and has also a locomotive. Print the train in the console.
Exercise 2
Write a method that returns the Fibonacci sequence. The size of the sequence to be generated should be given as a parameter.
Exercise 3
Create a method that checks if 2 lists have the same items.
If the 2 lists have the same items, print in the console The lists are identical
otherwise print The lists are different
.
Exercise 4
Create a method that generates a matrix using Lists. The rows will have ascending sizes. First row will have size 1, second size 2 and so one. Give the number of rows as a parameter to the method. i.e. for a matrix of size 3, the matrix should look like this:
0
0 0
0 0 0
Homework
Implement a Java program that interleaves 2 lists in a third list. For example, given
listA = {1,2,3}
andlistB = {4,5,6}
,listC
should belistC = {1,4,2,5,3,6}
Given a list, print in the console the sorted list. Then, reverse the list and print it to the console.
Example:
Given the following list
list = {5, 2, 10, 6, 8, 3}
The sorted list will be:
sotedList = {2, 3, 5, 6, 8, 10}
The reversed list will be:
reveresedList = {10, 8, 6, 5, 3, 2}
Advanced
If you did OOP before, you can try to do some exercises with an actual implementation of the list.