1 of 40

For Loops

2 of 40

Strings and the equals Method

String a = “Hello”;

String b = “Hello”;

if( a.equals(b) )

{

// a and b have the same characters in the same order!

}

3 of 40

Strings and the equals Method

String a = “Hello”;

String b = “Hello”;

if( a.equals(b) )

{

// a and b have the same characters in the same order!

}

a == b

does not always work as you expect (because reasons)

4 of 40

For Loops

for( int i=0; i < 10; i++ )

{

System.out.println( i );

}

For i,

starting at 0,

as long as it is less than 10,

print it

then increment it.

5 of 40

For Loops

for( int i=0; i < 10; i++ )

{

System.out.println( i );

}

For i,

starting at 0,

as long as it is less than 10,

print it

then increment it.

6 of 40

For Loops

for( int i=0; i < 10; i++ )

{

System.out.println( i );

}

For i,

starting at 0,

as long as it is less than 10,

print it

then increment it.

7 of 40

For Loops

for( int i=0; i < 10; i++ )

{

System.out.println( i );

}

For i,

starting at 0,

as long as it is less than 10,

print it

then increment it.

8 of 40

For Loops

for( int i=0; i < 10; i++ )

{

System.out.println( i );

}

For i,

starting at 0,

as long as it is less than 10,

print it

then increment it.

9 of 40

For Loops

for( int i=0; i < 10; i++ )

{

System.out.println( i );

}

For i,

starting at 0,

as long as it is less than 10,

print it

then increment it.

10 of 40

Syntax

for(initialize counter; condition; increment)

{

// body

}

for(int i = 0; i < 5; i++)

{

System.out.println( i );

}

0

1

2

3

4

Smallest: 0

Largest: 4

11 of 40

For Loops & For Each Loops

Foreach Loop

for(Type var : list)

Iterates over every element of an ArrayList

Always starts at the beginning of list

Always stops at the end of list�

Stores the value of each element in var

For Loop

for(int i = 0; i < limit; i++)

Iterates over numbers

Specify where to start

Specify where to end

Specify how to increment the value each iteration (i++, i+=2, i--)

12 of 40

Funsheet!

13 of 40

For loops and ArrayLists

for(String s : list)

{

System.out.println(s);

}

for(int i=0; i<list.size(); i++)

{

String s = list.get(i);

System.out.println(s);

}

ArrayList<String> list = new ArrayList<String>();

14 of 40

Example (Similarities)

for(String s : list)

{

System.out.println(s);

}

for(int i=0; i<list.size(); i++)

{

String s = list.get(i);

System.out.println(s);

}

ArrayList<String> list = new ArrayList<String>();

15 of 40

Example (Similarities)

for(String s : list)

{

System.out.println(s);

}

for(int i=0; i<list.size(); i++)

{

String s = list.get(i);

System.out.println(s);

}

ArrayList<String> list = new ArrayList<String>();

16 of 40

Example (Similarities)

for(String s : list)

{

System.out.println(s);

}

for(int i=0; i<list.size(); i++)

{

String s = list.get(i);

System.out.println(s);

}

ArrayList<String> list = new ArrayList<String>();

17 of 40

Example (Differences)

for(String s : list)

{

System.out.println(s);

}

for(int i=0; i<list.size(); i++)

{

String s = list.get(i);

System.out.println(s);

}

ArrayList<String> list = new ArrayList<String>();

For loops have access to the index being iterated over.

Foreach loops do not have access to the current element’s index

18 of 40

Example (Differences)

for(String s : list)

{

System.out.println(s);

}

for(int i=3; i<list.size()/2; i++)

{

String s = list.get(i);

System.out.println(s);

}

ArrayList<String> list = new ArrayList<String>();

For loops can iterate over a specific range of values

Foreach loops always iterate over ALL values

19 of 40

Example (Differences)

for(String s : list)

{

System.out.println(s);

}

for(int i=list.size()-1; i>=0; i--)

{

String s = list.get(i);

System.out.println(s);

}

ArrayList<String> list = new ArrayList<String>();

For loops can iterate BACKWARDS

Foreach loops always iterate forwards

20 of 40

For Loops & For Each Loops

Foreach Loop

for(Type var : list)

Simple way to access all elements of a List

Simple syntax

For Loop

for(int i = 0; i < limit; i++)

More control

More complicated syntax

21 of 40

Writing a for loop

  1. What list are you iterating over?

ArrayList<String> blah = …

22 of 40

Writing a for loop

  1. What list are you iterating over?
  2. What index do you want to start at?

ArrayList<String> blah = …

for(int i=0;

23 of 40

Writing a for loop

  1. What list are you iterating over?
  2. What index do you want to start at?
  3. What index do you want to end at?

ArrayList<String> blah = …

for(int i=0; i<blah.size();

24 of 40

Writing a for loop

  1. What list are you iterating over?
  2. What index do you want to start at?
  3. What index do you want to end at?
  4. Get the value at index i and store it in a variable

ArrayList<String> blah = …

for(int i=0; i<blah.size(); i++)

{

String thing = blah.get(i);

}

25 of 40

Writing a for loop

  1. What list are you iterating over?
  2. What index do you want to start at?
  3. What index do you want to end at?
  4. Get the value at index i and store it in a variable
    1. What type should the variable be?

ArrayList<String> blah = …

for(int i=0; i<blah.size(); i++)

{

String thing = blah.get(i);

}

26 of 40

Writing a for loop

  1. What list are you iterating over?
  2. What index do you want to start at?
  3. What index do you want to end at?
  4. Get the value at index i and store it in a variable
    1. What type should the variable be?
  5. Do something with that variable

ArrayList<String> blah = …

for(int i=0; i<blah.size(); i++)

{

String thing = blah.get(i);

System.out.println(thing);

}

27 of 40

Bonus Funsheet!

28 of 40

Searching a List

Consider the following ArrayList

ArrayList<String> letters = new ArrayList<String>();

letters.add(“a”);

letters.add(“e”);

letters.add(“i”);

letters.add(“o”);

letters.add(“u”);

0

1

2

3

4

a

e

i

o

u

29 of 40

Searching a List

Consider the following ArrayList

ArrayList<String> letters = new ArrayList<String>();

letters.add(“a”);

letters.add(“e”);

letters.add(“i”);

letters.add(“o”);

letters.add(“u”);

0

1

2

3

4

a

e

i

o

u

Where is the letter “o” in this list?

30 of 40

Searching a List

Consider the following ArrayList

ArrayList<String> letters = new ArrayList<String>();

letters.add(“a”);

letters.add(“e”);

letters.add(“i”);

letters.add(“o”);

letters.add(“u”);

0

1

2

3

4

a

e

i

o

u

What is an algorithm you could use to programmatically find the letter “o” in a list?

31 of 40

Searching a List

Consider the following ArrayList

ArrayList<String> letters = new ArrayList<String>();

letters.add(“a”);

letters.add(“e”);

letters.add(“i”);

letters.add(“o”);

letters.add(“u”);

0

1

2

3

4

a

e

i

o

u

Where is the letter “x” in this list?

32 of 40

Searching a List

Consider the following ArrayList

ArrayList<String> letters = new ArrayList<String>();

letters.add(“a”);

letters.add(“e”);

letters.add(“i”);

letters.add(“o”);

letters.add(“u”);

0

1

2

3

4

a

e

i

o

u

If you can’t find a value in a list, the default “does not exist” index is -1

33 of 40

Activity: Searching a List

Complete the following method that returns the index of target in list,

or -1 if list does not contain target.

public int find(String target, ArrayList<String> list)

{

int ret = ____________

for(_________________________________)

{

� }

return ret;

}

34 of 40

Activity: Searching a List

Complete the following method that returns the index of target in list,

or -1 if list does not contain target.

public int find(String target, ArrayList<String> list)

{

int ret = -1; // default value if we can’t find target

for(int i=0; i<list.size(); i++) // iterate over every element

{

String letter = list.get(i); // always get the value at i and store in variable

if( letter.equals(target) ) // use .equals method to compare strings

{ ret = i; } // if the strings match, remember this i� }

return ret;

}

35 of 40

Counting

Consider the following ArrayList

ArrayList<String> letters = new ArrayList<String>();

letters.add(“b”);

letters.add(“a”);

letters.add(“n”);

letters.add(“a”);

letters.add(“n”);

letters.add(“a”);

0

1

2

3

4

5

b

a

n

a

n

a

36 of 40

Counting

Consider the following ArrayList

ArrayList<String> letters = new ArrayList<String>();

letters.add(“b”);

letters.add(“a”);

letters.add(“n”);

letters.add(“a”);

letters.add(“n”);

letters.add(“a”);

0

1

2

3

4

5

b

a

n

a

n

a

How many times does “a”

appear in this list?

37 of 40

Counting

Consider the following ArrayList

ArrayList<String> letters = new ArrayList<String>();

letters.add(“b”);

letters.add(“a”);

letters.add(“n”);

letters.add(“a”);

letters.add(“n”);

letters.add(“a”);

0

1

2

3

4

5

b

a

n

a

n

a

What is an algorithm you could use to programmatically count how often “a” is in a list?

38 of 40

Activity: Counting Elements in a List

Complete the following method that returns how many times target appears in list,

public int count(String target, ArrayList<String> list)

{

int ret = ____________

for(_________________________________)

{

� }

return ret;

}

39 of 40

Activity: Counting Elements in a List

Complete the following method that returns how many times target appears in list,

public int count(String target, ArrayList<String> list)

{

int ret = 0; // default value if target doesn’t exist, the count is 0

for(int i=0; i<list.size(); i++) // iterate over every element

{

String letter = list.get(i); // always get the value at i and store in variable

if( letter.equals(target) ) // use .equals method to compare strings

{ ret++; } // if the strings match, increment i� }

return ret;

}

40 of 40

Lab 3.2