Cards from Crash Course Flashcards
Can you access List items by position?
Yes
Can you add items to the end of a list, or in the middle? or the beginnin?
Yes
Can you replace items on the array at a certain position?
Yes
Do you access the number of items in a list by .size() or .length()?
.size()
Does this compile? What is “List”?
Yes. An interface.
What’s the method used to put items on a List?
.add();
How would you find something on a list by an index? Which method would you use?
.get(0);
Why doesn’t this compile?
List names = new ArrayList<>();
names.add(“Jedd”);
names.add(“Lomin”);
String name =names.get(0);
The object in names may not always be a string, so the compiler will work to prevent you from messing up. If you went List it would work.
Will this compile?
List names = new ArrayList<>();
names.add(“Jedd”);
names.add(“Lomin”);
String name = names.get(0);
no
names.get(0); may not always be a String so the compiler will not let you do this. If you gave List names a type of String (List names) then you would be fine.
What is the classCastException about?
It’s when you try to cast something into a data type that it can’t be cast into.
Does this work?
List names = new ArrayList<>();
names.add(“Jedd”);
names.add(“Lomin”);
String name = names.get(0); System.out.println(names);
yeah that’s fine
Does this compile? why or why not?
List numbers = new ArrayList<>();
cannot make a list of a primitive
Does this compile?
List names = new ArrayList<>();
names.add(“Jedd”);
names.add(“Lomin”);
String name = names.get(0); System.out.println(names); System.out.println(names.contains("Jedd"));
Yes
Under what condition would you not be able to sue the contains() method?
If the object doesn’t implement that method.
Does this compile?
public static void main(String… strings) {
List names = new ArrayList<>();
names.add(“Jedd”);
names.add(“Lomin”);
System.out.println(getLongNames(names).size());
}
public static List getLongNames(List list){ List results = new ArrayList<>(list); return results; }
Yes. It’s fine. Demonstartes how you can build a list with an argument.