break
Statementcontinue
Statementswitch
StatementHomework 8 is due this Sunday at 11:59 PM.
You will find the assignment here.
If you have a problem or a question, make a post on the Class Discussion Area.
Method | Description |
---|---|
add(value) | Adds a value to the end of the list |
add(index, value) | Adds a value at a given position |
clear() | Removes all entries from the list |
get(index) | Returns the value at a given position |
remove(index) | Remove the element at a given position |
set(index, value) | Changes the value at a give position |
size() | Returns the number of elements in the list |
import java.util.ArrayList; public class ListAdd { public static void main (String[] args){ ArrayList<String> list = new ArrayList<>(); System.out.println(list.size()); list.add("one"); System.out.println(list.size()); list.add("two"); System.out.println(list.size()); System.out.println(list.size()); System.out.println(list); } } $ java ListAdd 0 1 2 3 [one, two, three]
import java.util.ArrayList;
public class ListAddIndex {
public static void main (String[] args){
ArrayList<String> list = new ArrayList<>();
list.add("one");
list.add("two");
list.add("three");
System.out.println(list);
list.add(1, "one and a half");
System.out.println(list);
}
}
$ java ListAddIndex
[one, two, three]
[one, one and a half, two, three]
import java.util.ArrayList;
public class ListClear {
public static void main (String[] args){
ArrayList<String> list = new ArrayList<>();
list.add("one");
list.add("two");
list.add("three");
System.out.println(list);
list.clear();
System.out.println(list);
}
}
$ java ListClear
[one, two, three]
[]
import java.util.ArrayList;
public class ListGet {
public static void main (String[] args){
ArrayList<String> list = new ArrayList<>();
list.add("one");
list.add("two");
list.add("three");
System.out.println(list);
System.out.println("The value at index 1: " + list.get(1));
}
}
$ java ListGet
[one, two, three]
The value at index 1: two
import java.util.ArrayList;
public class ListRemove {
public static void main (String[] args){
ArrayList<String> list = new ArrayList<>();
list.add("one");
list.add("two");
list.add("three");
System.out.println(list);
list.remove(1);
System.out.println(list);
}
}
$ java ListRemove
[one, two, three]
[one, three]
import java.util.ArrayList;
public class ListSet {
public static void main (String[] args){
ArrayList<String> list = new ArrayList<>();
list.add("one");
list.add("two");
list.add("three");
System.out.println(list);
list.set(1, "2");
System.out.println(list);
}
}
$ java ListSet
[one, two, three]
[one, 2, three]
import java.util.ArrayList; public class ListSize { public static void main (String[] args){ ArrayList<String> list = new ArrayList<>(); list.add("one"); System.out.println("Elements: " + list.size()); list.add("two"); System.out.println("Elements: " + list.size()); list.add("three"); System.out.println("Elements: " + list.size()); } } $ java ListSize Elements: 1 Elements: 2 Elements: 3
Method | Description |
---|---|
contains(value) | Returns true if the value can be found in the list |
indexOf(value) | Returns the index of the first occurence of a value in the list or -1 if the value cannot be found |
lastIndexOf(value) | Returns the index of the first occurence of a value in the list or -1 if the value cannot be found |
import java.util.ArrayList; public class ListContains { public static void main (String[] args){ ArrayListt<String> list = new ArrayList<>(); list.add("one"); list.add("two"); list.add("three"); System.out.println("\"two\" is in list: " + list.contains("two")); System.out.println("\"four\" is in list: " + list.contains("four")); } } $ java ListContains "two" is in list: true "four" is in list: false
import java.util.ArrayList; public class ListIndexOf { public static void main (String[] args){ ArrayList<String> teams = new ArrayList<>(); teams.add("Red Sox"); teams.add("Pats"); teams.add("Celtics"); teams.add("Bruins"); teams.add("Red Sox"); System.out.println(teams); System.out.println("Index of \"Red Sox\": " + teams.indexOf("Red Sox")); System.out.println("Index of \"Yankees\": " + teams.indexOf("Yankees")); } } java ListIndexOf [Red Sox, Pats, Celtics, Bruins, Red Sox] Index of "Red Sox": 0 Index of "Yankees": -1
import java.util.ArrayList; public class ListLastIndexOf { public static void main (String[] args){ ArrayList<String> teams = new ArrayList<>(); teams.add("Red Sox"); teams.add("Pats"); teams.add("Celtics"); teams.add("Bruins"); teams.add("Red Sox"); System.out.println(teams); System.out.println("Last index of \"Red Sox\": " + teams.lastIndexOf("Red Sox")); System.out.println("Last index of \"Yankees\": " + teams.lastIndexOf("Yankees")); } } $ java ListLastIndexOf [Red Sox, Pats, Celtics, Bruins, Red Sox] Last index of "Red Sox": 4 Last index of "Yankees": -1
import java.io.*; import java.util.*; public class FileToList { public static void main (String[] args) throws FileNotFoundException { ArrayList<String> list = create_list_from_file(args[0]); System.out.println(list); } public static ArrayList>String> create_list_from_file(String filename) throws FileNotFoundException { File file = new File(filename); ArrayList>String> list = new ArrayList<>(); Scanner input = new Scanner(file); while (input.hasNext()){ list.add(input.next()); } return list; } } $ java FileToList.java fruit.txt [grapes, pears, oranges, cranberries, apples, melons, blueberries]
for
loop it tells the number of times the loop
will run
while
and do
/while
loops
it gives the condition that will make the loop stop
continue
break
break
Statementbreak
statement
continue
Statementcontinue
statement switch
Statementswitch
statement
switch(EXPRESSION) {
case VALUE_1:
STATEMENT;
STATEMENT;
...
break;
case y:
STATEMENT;
STATEMENT;
break;
...
default:
STATEMENT;
STATEMENT;
}
import java.awt.Point;
Point p = new Point(3, 8);
System.out.println(p);
java.awt.Point[x=3,y=8]
Point
object we can use the translate method
which takes two arguments
p.translate(–1, –2);
import java.awt.Point; public class PointExample { public static void main(String[] args) { Point p = new Point(3, 8); System.out.println("initially p = " + p); p.translate(-1, -2); System.out.println("after translating p = " + p); } } $ java PointExample initially p = java.awt.Point[x=3,y=8] after translating p = java.awt.Point[x=2,y=6]
Method | Description |
---|---|
translate(dx, dy) | Translates the coordinates by the given amounts |
setLocation(x, y) | Sets the coordinates to the given values |
distance(p2) | Returns the distance from this point to p2 |
int sum = p.x + p.y; System.out.println("Sum of coordinates = " + sum);
p.x = 12; p.y = 15;
public class CLASS_NAME {
FIELD
FIELD
...
METHOD
METHOD
...
}
CLASS_NAME.java
DATA_TYPE FIELD_NAME;
Type | Value |
---|---|
int | 0 |
double | 0.0 |
char | '\0' |
boolean | false |
public class Point1 { int x; int y; }
p.x = 7; p.y = 2;
public class Point1Driver{ public static void main(String[] args) { Point1 p = new Point1(); p.x = 7; p.y = 2; System.out.println("p is (" + p.x + ", " + p.y + ")"); } } $ java Point1Driver p is (7, 2)
public DATA_TYPE METHOD_NAME(DATA_TYPE PARAMTER_NAME, ...) {
STATEMENT;
...
}
public void translate(int dx, int dy)
p.translate(1, -3);
static
keyword
public
keywordvoid
public void translate(int dx, int dy) {
add dx to this Point object’s x value.
add dy to this Point object’s y value.
}
public class Point2{ int x; int y; public void translate(int dx, int dy) { x += dx; y += dy; } }
public class Point2Driver{ public static void main(String[] args) { Point2 p = new Point2(); p.x = 7; p.y = 2; System.out.println("p is (" + p.x + ", " + p.y + ")"); p.translate(-2, 3); System.out.println("p is (" + p.x + ", " + p.y + ")"); } } $ java Point2Driver p is (7, 2) p is (5, 5)