Lecture 3 Flashcards
What are the benefits and drawbacks of (univariate) arrays?
Benefits: they are fast to access
Drawbacks: they’re a fixed length, making them waste memory space, difficult to maintain multiple arrays (keeping them synchronised). Needs clean data.
What are the drawbacks of (multivariate) arrays?
All the values should have the same data type (int or String)
When dealing with large data sets, what can a class do?
It can be designed to be lightweight, meaning that we shouldn’t implement getters or setters. This is a sensible choice, if we’re dealing with a class whose purpose is to just store data (does not obey encapsulation)
- also it stores the data in the attributes that can be of different types
What does the record keyword do?
It provides a constructor signature after the record name. it has immutable attributes defined in parentheses after the record name. it provides publicly accessible methods for reading (not setting) their attributes.
Give an example of the record keyword.
record City(int id, int year, String city) {
//…
}
When are records used?
Usually for creating immutable data classes.
What can a list do?
Lists (is a collection) are dynamic, and therefore not of fixed length. It holds elements in a specific order and allows duplicates.
(ArrayList and LinkedList)
Give an example of a list.
List<CityRecord> allRecords =
new ArrayList<>();</CityRecord>
What can a set do?
Sets don’t have any order of entry, but ensures that there are no duplicates. You can override the equals() method, if you need the same value multiple times.
(HashSet)
What can a map do?
Map defines relations from key to value. This allows for easy lookup. The value may be a collection itself (e.g. list) when a key is related to multiple values.
(HashMap)
How do you read a txt file?
Instantiate the File, instantiate the FileReader, instantiate the BufferedReader. Try to read the line and process it an repeat until there’s nothing left. Then close the file. If there are errors, catch the errors.
How do you read a csv file?
(Use CSV Parser)
Instantiate the File, then iterate over the CSVRecord and parse it. Then make a for each loop. This is more simple, stable and can solve tricky cases and supports many formats.
What does XML mean?
Extensible Markup Language. Support built-in for Java
What does JSON mean?
Java Script Object Notation. Many parsers available, e.g. Google gson
True or false: Structured data formats include CSV, XML, JSON and PDF containing natural language.
False