Docsity

Notes on Representing Lists in Java - Introduction to Computer Science | CS 110, Study notes of Computer Science

Toggle sidebar
bg1
CS 110: Introduction to Computer Science Spring 2008
Representing Lists in Java
Java provides a few ways to represent lists, including the ‘array’ which his part of the
Java language proper, and a number of classes in the Java library. Perhaps the easiest
way to represent lists is with a library class called ArrayList. Take a look at the
following Java and Python samples:
Java: http://www.cs.usfca.edu/~wolber/courses/110.f07/javaSa mples/ListTest.java
Python: http://www.cs.usfca.edu/~wolber/courses/110.f07/ javaSamples/ListTest.py
Here's the key differences in terms of how you do things:
Java ArrayList Python list
ArrayList<type> list = new ArrayList<type>() list=[]
list.add(object) list.append(object)
list.get(i) list[i]
list.size() len(list)
list2= list.clone() list2=list[:]
ArrayList requires the type of the elements in the array to be specified (just like
everything in Java). Consider how you declare and set an integer in Java:
int x; // declare x as an integer
x= 4; // set x to 4
ArrayList is similar but you declare that the variable is a list of a specific type using <>
ArrayList<String> list = new ArrayList<String>
list.add("abc");
ArrayList can only hold object types, not scalers like 'int'.
ArrayList is not 'built-in' to the language, but part of the core java library (must import
java.util.*)
Instructor Coding Sample
Write a class Person and a main program that creates three person’s, puts them in a list,
and totals up the age of each person in the list.
In Class Programming Assignment:
To practice using ArrayList and get started with the ParkingLot program, write the
following code in the files Car.java and ParkingLot.java:
pf2

Partial preview of the text

Download Notes on Representing Lists in Java - Introduction to Computer Science | CS 110 and more Study notes Computer Science in PDF only on Docsity!

CS 110: Introduction to Computer Science Spring 2008

Representing Lists in Java

Java provides a few ways to represent lists, including the ‘array’ which his part of the Java language proper, and a number of classes in the Java library. Perhaps the easiest way to represent lists is with a library class called ArrayList. Take a look at the following Java and Python samples: Java: http://www.cs.usfca.edu/~wolber/courses/110.f07/javaSamples/ListTest.java Python: http://www.cs.usfca.edu/~wolber/courses/110.f07/javaSamples/ListTest.py Here's the key differences in terms of how you do things: Java ArrayList Python list ArrayList list = new ArrayList() list=[] list.add(object) list.append(object) list.get(i) list[i] list.size() len(list) list2= list.clone() list2=list[:] ArrayList requires the type of the elements in the array to be specified (just like everything in Java). Consider how you declare and set an integer in Java: int x; // declare x as an integer x= 4; // set x to 4 ArrayList is similar but you declare that the variable is a list of a specific type using <> ArrayList list = new ArrayList list.add("abc"); ArrayList can only hold object types, not scalers like 'int'. ArrayList is not 'built-in' to the language, but part of the core java library (must import java.util.*)

Instructor Coding Sample

Write a class Person and a main program that creates three person’s, puts them in a list, and totals up the age of each person in the list.

In Class Programming Assignment:

To practice using ArrayList and get started with the ParkingLot program, write the following code in the files Car.java and ParkingLot.java:

CS 110: Introduction to Computer Science Spring 2008

  • A class Car with three public data members: initialSpot, currentSpot and finalSpot, all of type integer.
  • A class ParkingLot with a main method that creates three cars, puts them in a list named ‘waitingList’, and prints out the list. Next steps: Add a constructor to Car Define ‘waitingList’ as a data member of class ParkingLot instead of a local. Read in the initial spot for each car using Scanner class