Java I/O and Arrays: Reading Files and Dynamic Allocation, Study notes of Computer Science

How to read input files in java using a scanner object and the file class. It also covers java's dynamic array allocation and the use of arrays of primitives and objects. The vector class is introduced as a collection class that can store different types of objects and grow and shrink dynamically.

Typology: Study notes

Pre 2010

Uploaded on 02/24/2010

koofers-user-3c4
koofers-user-3c4 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Reading from Input Files in Java
This can be done extremely similarly to reading from the
keyboard. A Scanner object can still be used. But, instead of
passing System.in into the constructor, a File object must be
passed in:
Scanner fin = new Scanner(new File("anyfile.txt"));
In order to create a File object, the File constructor must be
called. This constructor simply takes in a String that stores the
name of the file. Thus, in a single statement, we can set a
Scanner object to read from a file instead of the keyboard.
Once the Scanner object is "set up," then you use it
EXACTLY as we have been using Scanner objects until now.
Use the following methods: next(), nextInt(), and nextDouble().
(There are other methods, but these are the ones you'll be
using most frequently.)
The only difference between reading from the keyboard and a
file is that you should close a file once you are done reading
from it. Thus, you must close the appropriate Scanner object.
The name of the method is close and it takes no parameters:
fin.close();
pf3
pf4
pf5

Partial preview of the text

Download Java I/O and Arrays: Reading Files and Dynamic Allocation and more Study notes Computer Science in PDF only on Docsity!

Reading from Input Files in Java

This can be done extremely similarly to reading from the keyboard. A Scanner object can still be used. But, instead of passing System.in into the constructor, a File object must be passed in: Scanner fin = new Scanner(new File("anyfile.txt")); In order to create a File object, the File constructor must be called. This constructor simply takes in a String that stores the name of the file. Thus, in a single statement, we can set a Scanner object to read from a file instead of the keyboard. Once the Scanner object is "set up," then you use it EXACTLY as we have been using Scanner objects until now. Use the following methods: next(), nextInt(), and nextDouble(). (There are other methods, but these are the ones you'll be using most frequently.) The only difference between reading from the keyboard and a file is that you should close a file once you are done reading from it. Thus, you must close the appropriate Scanner object. The name of the method is close and it takes no parameters: fin.close();

Arrays in Java

Just like all other non-primitives in Java, arrays are references. Unlike C, all arrays are dynamically allocated. This means that the size of an array is always specified at run-time. Since a dynamic allocation is done, we use the new keyword as follows: int[] values = new int[10]; This allocates an array of size 10 that values references. Consider the following picture: From this point, we use arrays of primitives exactly as they would be used in C. To reference an individual array slot, we use the [] operator. The following segment of code initializes each slot in the array values to 0: for (int i=0; i<values.length; i++) values[i] = 0; One key idea to notice here that is different than C is that you can access the length of an array, using the length field. Note that length is a public variable for all arrays. Thus, there are no parentheses (which would indicate a method call). Let's look at an example that utilizes Java's dynamic array allocation capabilities.

Vector Class

Java provides a few classes that allow us to manage collections of objects. Today, we'll discuss the Vector class, which stores a group of objects. One of the "limitations" of an array in C is that it can only store one type of object. In Java, a Vector can store different types of objects. (Actually so can an array.) Secondly, arrays in C are of a fixed size. (Actually you can also declare dynamically allocated arrays in C.) The Vector class in Java essentially handles both of these issues very well. A Vector object can arbitrarily grow and shrink as needed without the user worrying about any of the details. Furthermore, a Vector can store different types of objects. Well, this is a bit of a stretch actually. The manner in which Java gets away with having Vectors that can store different types of objects is through its "tree" of inheritance. All non- primitives are objects, and some objects inherit from others. For example, if I wrote a Car class, I could write a SportsCar class that inherited all the methods and variables of the Car class, but then maybe had some extra variables and methods that were specific to the SportsCar. Furthermore, then I could maybe have a Corvette class that inherited from SportsCar. It would retain all the attributes of the SportsCar class, but then have some of its extra attributes. In this example, we have the following: SportsCar inherits from Car, so a SportsCar object IS-A Car object. Corvette inherits from SportsCar, so a Corvette object IS-A SportsCar object AND a Car object.

Java also provides a very basic class called Object. Every non- primitive class automatically inherits from Object. Thus, in a way, all non-primitives are valid "Object" objects. Thus, if we create an array of Object or a Vector of Object, then that collection can store ANY sort of non-primitive, ranging from a Time object, to a String object, to a Corvette object. In the old version of Java, the way to create an empty Vector was as follows: Vector v = new Vector(); This looks like a regular default constructor. In the new version of Java, templates are used. This essentially means that some classes can be based upon a "template" class, where you fill in the class you desire. For a Vector object, you are allowed to declare a Vector of any type of class. Here is how we can declare a Vector of class Object: Vector v = new Vector(); Thus, added to the name of the class is , where T is replaced with the type of object you want the vector to store. If you wanted to create a Vector of String objects, you'd do the following: Vector v = new Vector();

Some Details about Using a Vector

Most of the methods are quite self-explanatory, but a couple things need to be mentioned:

  1. When values are extracted from a Vector of Object(s), how can we make them the correct type?
  2. Can we insert elements in arbitrary indices, or do the elements always have to occupy the first set of indices, starting at 0? In order to properly use an Object extracted from a Vector, you must cast it to the appropriate class, like so: String middle = (String)v.elementAt(1); In this example, the extracted item is an Object, but is also known to be a String. Casting it to a string allows us to have a String reference, middle, reference it. Note that the cast is NOT necessary when adding Strings into the Vector. The answer to the second question is no. You are not allowed to insert an element into index 20, if indices 0 through 19 aren't already filled.