HW0B
约 401 个字 49 行代码 预计阅读时间 2 分钟
Note:
- Java has
null
instead ofNone
in python! - In
new int[3]
, theint
is the type in the array; and3
is the length. With this syntax, all elements take on their "default value". Forint
, this is 0. - Arrays do not print nicely, for reasons beyond the scope of HW 0. To print an array, you can call
Arrays.toString(array)
. - Use
myArray.length
instead ofmyArray.length()
to get the length of the array. Java doesn't have the methodlength()
Foreach Loop / Enhanced For Loop
in python:
lst = [1, 2, 3]
for i in lst:
print(i)
in java:
int[] array = {1, 2, 3};
for (int i : array) {
System.out.println(i);
}
- Notice the type declaration of the iterating variable, as well as the usage of
:
instead ofin
. - We can also use this syntax on certain other types, such as
List
s andSet
s.
Lists (resizable)
python:
lst = []
lst.append("zero")
lst.append("one")
lst[0] = "zed"
print(l[0])
print(len(l))
if "one" in lst:
print("one in lst")
for elem in lst:
print(elem)
java:
List<String> lst = new ArrayList<>();
lst.add("zero");
lst.add("one");
lst.set(0, "zed");
System.out.println(lst.get(0));
System.out.println(lst.size());
if (lst.contains("one")) {
System.out.println("one in lst");
}
for (String elem : lst) {
System.out.println(elem);
}
- Java has the
List
interface. We largely use theArrayList
implementation. - The
List
interface is parameterized by the type it holds, using the angle brackets<
and>
. List
s, again, do not support slicing or negative indexing.
Sets
java:
Set<Integer> set = new HashSet<>();
set.add(1);
set.add(1);
set.add(2);
set.remove(2);
System.out.println(set.size());
if (set.contains(1)) {
System.out.println("1 in set");
}
for (int elem : set) {
System.out.println(elem);
}
Set->集合
- Java has the
Set
interface. There are two main implementations:TreeSet
, andHashSet
.TreeSet
keeps its elements in "sorted" order, and is fast. In contrast,HashSet
does not have a defined "order", but is (usually) really fast.- We will formalize these notions of "fast" later on in the course when we learn about asymptotic analysis.
- A
Set
cannot contain duplicate items. If we try to add an item already in the set, nothing happens.
Dictionaries / Maps
d = {}
d["hello"] = "hi"
d["hello"] = "goodbye"
print(d["hello"])
print(len(d))
if "hello" in d:
print("\"hello\" in d")
for key in d.keys():
print(key)
Map<String, String> map = new HashMap<>();
map.put("hello", "hi");
map.put("hello", "goodbye");
System.out.println(map.get("hello"));
System.out.println(map.size());
if (map.containsKey("hello")) {
System.out.println("\"hello\" in map");
}
for (String key : map.keySet()) {
System.out.println(key);
}
- Java has the
Map
interface. There are two main implementations:TreeMap
, andHashMap
. Similarly to sets,TreeMap
keeps its keys sorted and is fast;HashMap
has no defined order and is (usually) really fast. - A
Map
cannot contain duplicate keys. If we try to add a key already in the map, the value is overwritten. - In the angle brackets, we have the "key type" first, followed by the "value type".
Map
s cannot directly be used with the:
for loop. Typically, we callkeySet
to iterate over a set of the keys, and use those to retrieve the values. One may also iterate over theentrySet
to get both the keys and values.
Classes
Main
Knowledge Learned
List<Integer> lst = new ArrayList<>();
is correctList<int>lst
is wrong!!!!!!!!!!