arrays

Refresher

Refresher

Refresher

int a = 9;
double d = 5.4;
char ch = 'x';
String str = "hello";

What do you do if you want to store many values?

You need one variable for each value

String colour01 = "red"
String colour02 = "blue"
String colour03 = "green"
String colour04 = "yellow"
String colour05 = "pink"

Can you use just one variable to hold many values?

How would you do it?

String colours = 
"red, blue, green, yellow, pink"

Issues?

  • Long string
  • How do you extract the values?
  • Integers?
 int
 a
 a = 
 1, 2, 3, 4, 5 ;
 int
 a
 a = 
 1, 2, 3, 4, 5
 { 
 };
 int
 a
 a = 
 1, 2, 3, 4, 5
 { 
 };
 []
 int
 a
 a = 
 1, 2, 3, 4, 5
 { 
 };
 []
String[] colours = {"red", "green", "blue", "yellow", "pink"};

char[] letters = {"a", "b"};

boolean[] a = {false, true, true, false, false};

float[] numbers = {3.14159};
// VS Code Demonstration

int[] a = {1,2,3,4,5}

System.out.println(a[0]);
System.out.println(a[2]);




System.out.println(colours[3]);

{"red", 3, 3.45, 'c'}

Problem solving

Problem solving

We want to schedule a weekly meetup for our group. To work out the best day to meet we started a poll asking the group members to choose one day of the week.

From the poll result, how do we work out the best day?

Problem solving

int mondayVote = ?;
int tuesdayVote = ?;
int wednesdayVote = ?;
int thursdayVote = ?;
int fridayVote = ?;
int saturdayVote = ?;
int sundayVote = ?;

Without arrays, you would need 7 variables, one for each day in the week

string bestDay = "Monday";
int mostVotes = mondayVote;

if (tuesdayVote > mostVotes){
  mostVotes = tuesdayVote;
  bestDay = "Tuesday";
}
if (wednesdayVote > mostVotes){
  mostVotes = wednesdayVote;
  bestDay = "Wednesday";
}
if (thursday > mostVotes){
  mostVotes = thursdayVote;
  bestDay = "Thursday";
}
...
if (sundayVote > mostVotes){
  mostVotes = sundayVote;
  bestDay = "Sunday";
}

Without arrays, you would need 7 variables, one for each day in the week

What if instead finding the best day in a given week, you have to find the best day in a given month?

What if instead finding the best day in a given week, you have to find the best day in a given month?

What if instead finding the best day in a given week, you have to find the best day in a given month?

What about the best day in a given year?

What about the best day in a given year?

i

mostVotes

days[i]

 mostVotes < days[i]

0

0

6

true

1

6

0

false

2

6

13

true

3

13

12

false

4

13

2

false

5

13

18

true

6

18

10

false

7

18

int[] days = {6, 0, 13, 12, 2, 18, 10};

int mostVotes = 0;
for (int i = 0; i < days.length; i++){
  if (days[i] > mostVotes){
  	mostVotes = days[i];
  }
}

i

mostVotes

days[i]

 mostVotes < days[i]

0

0

6

true

1

6

0

false

2

6

13

true

3

13

12

false

4

13

2

false

5

13

18

true

6

18

10

false

7

18

int[] days = {6, 0, 13, 12, 2, 18, 10};

int mostVotes = 0;
for (int i = 0; i < days.length; i++){
  if (days[i] > mostVotes){
  	mostVotes = days[i];
  }
}

i

mostVotes

days[i]

 mostVotes < days[i]

0

0

6

true

1

6

0

false

2

6

13

true

3

13

12

false

4

13

2

false

5

13

18

true

6

18

10

false

7

18

int[] days = {6, 0, 13, 12, 2, 18, 10};

int mostVotes = 0;
for (int i = 0; i < days.length; i++){
  if (days[i] > mostVotes){
  	mostVotes = days[i];
  }
}

i

mostVotes

days[i]

 mostVotes < days[i]

0

0

6

true

1

6

0

false

2

6

13

true

3

13

12

false

4

13

2

false

5

13

18

true

6

18

10

false

7

18

int[] days = {6, 0, 13, 12, 2, 18, 10};

int mostVotes = 0;
for (int i = 0; i < days.length; i++){
  if (days[i] > mostVotes){
  	mostVotes = days[i];
  }
}

i

mostVotes

days[i]

 mostVotes < days[i]

0

0

6

true

1

6

0

false

2

6

13

true

3

13

12

false

4

13

2

false

5

13

18

true

6

18

10

false

7

18

int[] days = {6, 0, 13, 12, 2, 18, 10};

int mostVotes = 0;
for (int i = 0; i < days.length; i++){
  if (days[i] > mostVotes){
  	mostVotes = days[i];
  }
}

i

mostVotes

days[i]

 mostVotes < days[i]

0

0

6

true

1

6

0

false

2

6

13

true

3

13

12

false

4

13

2

false

5

13

18

true

6

18

10

false

7

18

int[] days = {6, 0, 13, 12, 2, 18, 10};

int mostVotes = 0;
for (int i = 0; i < days.length; i++){
  if (days[i] > mostVotes){
  	mostVotes = days[i];
  }
}

i

mostVotes

days[i]

 mostVotes < days[i]

0

0

6

true

1

6

0

false

2

6

13

true

3

13

12

false

4

13

2

false

5

13

18

true

6

18

10

false

7

18

int[] days = {6, 0, 13, 12, 2, 18, 10};

int mostVotes = 0;
for (int i = 0; i < days.length; i++){
  if (days[i] > mostVotes){
  	mostVotes = days[i];
  }
}

i

mostVotes

days[i]

 mostVotes < days[i]

0

0

6

true

1

6

0

false

2

6

13

true

3

13

12

false

4

13

2

false

5

13

18

true

6

18

10

false

7

18

int[] days = {6, 0, 13, 12, 2, 18, 10};

int mostVotes = 0;
for (int i = 0; i < days.length; i++){
  if (days[i] > mostVotes){
  	mostVotes = days[i];
  }
}

Minimal

By Daniel Sutantyo

Minimal

  • 8