ed in the lect

Download Report

Transcript ed in the lect

An array is a simple but powerful language construct used to
group and organize data.
An array is a simple but powerful language construct used to
group and organize data. When writing a program that manages
a large amount of information, such as a list of 100 numbers it is
not practical to declare separate variables for each piece of data.
An array is a simple but powerful language construct used to
group and organize data. When writing a program that manages
a large amount of information, such as a list of 100 numbers it is
not practical to declare separate variables for each piece of data.
Arrays solve this problem by letting us declare one variable that
can hold multiple, individually accessible values.
“An array is a list of values. Each value is stored at a specific, numbered position
in the array. The number corresponding to each position is called an index, or a
subscript.” (Lewis and Loftus, 4th edition).
“An array is a list of values. Each value is stored at a specific, numbered position
in the array. The number corresponding to each position is called an index, or a
subscript.” (Lewis and Loftus, 4th edition).
“An array is a sequence of values of the same data type. The data type can be
any Java primitive data type (such as int, double, char or boolean) or it can be
a user-defined type (a class).
“An array is a list of values. Each value is stored at a specific, numbered position
in the array. The number corresponding to each position is called an index, or a
subscript.” (Lewis and Loftus, 4th edition).
“An array is a sequence of values of the same data type. The data type can be
any Java primitive data type (such as int, double, char or boolean) or it can be
a user-defined type (a class). Each variable in the array, called an element, is
accessed using the array name and a subscript, called an index, which refers
to the element’s position in the array.” (Anderson and Franceschi)
“An array is a list of values. Each value is stored at a specific, numbered position
in the array. The number corresponding to each position is called an index, or a
subscript.” (Lewis and Loftus, 4th edition).
“An array is a sequence of values of the same data type. The data type can be
any Java primitive data type (such as int, double, char or boolean) or it can be
a user-defined type (a class). Each variable in the array, called an element, is
accessed using the array name and a subscript, called an index, which refers
to the element’s position in the array.” (Anderson and Franceschi)
“An array is a list of values. Each value is stored at a specific, numbered position
in the array. The number corresponding to each position is called an index, or a
subscript.” (Lewis and Loftus, 4th edition).
“An array is a sequence of values of the same data type. The data type can be
any Java primitive data type (such as int, double, char or boolean) or it can be
a user-defined type (a class). Each variable in the array, called an element, is
accessed using the array name and a subscript, called an index, which refers
to the element’s position in the array.” (Anderson and Franceschi)
“An array is a named collection of contiguous storage locations (storage locations
that are next to each other) that contain data items of the same type.
“An array is a list of values. Each value is stored at a specific, numbered position
in the array. The number corresponding to each position is called an index, or a
subscript.” (Lewis and Loftus, 4th edition).
“An array is a sequence of values of the same data type. The data type can be
any Java primitive data type (such as int, double, char or boolean) or it can be
a user-defined type (a class). Each variable in the array, called an element, is
accessed using the array name and a subscript, called an index, which refers
to the element’s position in the array.” (Anderson and Franceschi)
“An array is a named collection of contiguous storage locations (storage locations
that are next to each other) that contain data items of the same type. Arrays offer
a more streamlined way to store data than using individual data items (a distinct
variable would have to be defined/declared for each data item).” (Morelli, 3rd ed.)
“An array is a list of values. Each value is stored at a specific, numbered position
in the array. The number corresponding to each position is called an index, or a
subscript.” (Lewis and Loftus, 4th edition).
“An array is a sequence of values of the same data type. The data type can be
any Java primitive data type (such as int, double, char or boolean) or it can be
a user-defined type (a class). Each variable in the array, called an element, is
accessed using the array name and a subscript, called an index, which refers
to the element’s position in the array.” (Anderson and Franceschi)
“An array is a named collection of contiguous storage locations (storage locations
that are next to each other) that contain data items of the same type. Arrays offer
a more streamlined way to store data than using individual data items (a distinct
variable would have to be defined/declared for each data item).” (Morelli, 3rd ed.)
Arrays will remind you of Strings, in how they access their elements.
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Just like Strings, arrays are objects.
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Just like Strings, arrays are objects.
There are three steps to setting an array up to work with it:
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Just like Strings, arrays are objects.
There are three steps to setting an array up to work with it:
1. Declaration (come up with a name for the array).
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Just like Strings, arrays are objects.
There are three steps to setting an array up to work with it:
1. Declaration (come up with a name for the array).
int[ ] a;
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Just like Strings, arrays are objects.
There are three steps to setting an array up to work with it:
1. Declaration (come up with a name for the array).
At this stage the type of the elements is determined (ints, here).
int[ ] a;
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Just like Strings, arrays are objects.
There are three steps to setting an array up to work with it:
1. Declaration (come up with a name for the array).
At this stage the type of the elements is determined (ints, here).
int[ ] a;
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Just like Strings, arrays are objects.
There are three steps to setting an array up to work with it:
1. Declaration (come up with a name for the array).
At this stage the type of the elements is determined (ints, here).
(The meaning of the square brackets will be determined shortly).
int[ ] a;
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Just like Strings, arrays are objects.
There are three steps to setting an array up to work with it:
1. Declaration (come up with a name for the array).
At this stage the type of the elements is determined (ints, here).
(The meaning of the square brackets will be determined shortly).
2. Allocate (space for) the array.
int[ ] a;
a = new int[100];
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Just like Strings, arrays are objects.
There are three steps to setting an array up to work with it:
1. Declaration (come up with a name for the array).
At this stage the type of the elements is determined (ints, here).
(The meaning of the square brackets will be determined shortly).
2. Allocate (space for) the array.
int[ ] a;
a = new int[100];
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Just like Strings, arrays are objects.
There are three steps to setting an array up to work with it:
1. Declaration (come up with a name for the array).
At this stage the type of the elements is determined (ints, here).
(The meaning of the square brackets will be determined shortly).
2. Allocate (space for) the array.
int[ ] a;
a = new int[100];
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Just like Strings, arrays are objects.
There are three steps to setting an array up to work with it:
1. Declaration (come up with a name for the array).
At this stage the type of the elements is determined (ints, here).
(The meaning of the square brackets will be determined shortly).
2. Allocate (space for) the array.
int[ ] a;
a = new int[100];
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Just like Strings, arrays are objects.
There are three steps to setting an array up to work with it:
1. Declaration (come up with a name for the array).
At this stage the type of the elements is determined (ints, here).
(The meaning of the square brackets will be determined shortly).
2. Allocate (space for) the array.
3. Initialize (the elements of the) array.
int[ ] a;
a = new int[100];
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Just like Strings, arrays are objects.
There are three steps to setting an array up to work with it:
1. Declaration (come up with a name for the array).
At this stage the type of the elements is determined (ints, here).
(The meaning of the square brackets will be determined shortly).
2. Allocate (space for) the array.
3. Initialize (the elements of the) array.
int[ ] a;
a = new int[100];
for (int i = 0; i < a.length; i++) a[ i ] = (int)(Math.random()*10);
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Just like Strings, arrays are objects.
There are three steps to setting an array up to work with it:
1. Declaration (come up with a name for the array).
At this stage the type of the elements is determined (ints, here).
(The meaning of the square brackets will be determined shortly).
2. Allocate (space for) the array.
3. Initialize (the elements of the) array.
int[ ] a;
a = new int[100];
for (int i = 0; i < a.length; i++) a[ i ] = (int)(Math.random()*10);
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Just like Strings, arrays are objects.
There are three steps to setting an array up to work with it:
1. Declaration (come up with a name for the array).
At this stage the type of the elements is determined (ints, here).
(The meaning of the square brackets will be determined shortly).
2. Allocate (space for) the array.
3. Initialize (the elements of the) array.
int[ ] a;
a = new int[100];
for (int i = 0; i < a.length; i++) a[ i ] = (int)(Math.random()*10);
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Just like Strings, arrays are objects.
There are three steps to setting an array up to work with it:
1. Declaration (come up with a name for the array).
At this stage the type of the elements is determined (ints, here).
(The meaning of the square brackets will be determined shortly).
2. Allocate (space for) the array.
3. Initialize (the elements of the) array.
int[ ] a;
a = new int[100];
for (int i = 0; i < a.length; i++) a[ i ] = (int)(Math.random()*10);
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Just like Strings, arrays are objects.
There are three steps to setting an array up to work with it:
1. Declaration (come up with a name for the array).
At this stage the type of the elements is determined (ints, here).
(The meaning of the square brackets will be determined shortly).
2. Allocate (space for) the array.
3. Initialize (the elements of the) array.
int[ ] a;
a = new int[100];
for (int i = 0; i < a.length; i++) a[ i ] = (int)(Math.random()*10);
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Just like Strings, arrays are objects.
There are three steps to setting an array up to work with it:
1. Declaration (come up with a name for the array).
At this stage the type of the elements is determined (ints, here).
(The meaning of the square brackets will be determined shortly).
2. Allocate (space for) the array.
3. Initialize (the elements of the) array.
int[ ] a;
a = new int[100];
for (int i = 0; i < a.length; i++) a[ i ] = (int)(Math.random()*10);
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Just like Strings, arrays are objects.
There are three steps to setting an array up to work with it:
1. Declaration (come up with a name for the array).
At this stage the type of the elements is determined (ints, here).
(The meaning of the square brackets will be determined shortly).
2. Allocate (space for) the array.
3. Initialize (the elements of the) array.
int[ ] a;
a = new int[100];
for (int i = 0; i < a.length; i++) a[ i ] = (int)(Math.random()*10);
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Just like Strings, arrays are objects.
There are three steps to setting an array up to work with it:
1. Declaration (come up with a name for the array).
At this stage the type of the elements is determined (ints, here).
(The meaning of the square brackets will be determined shortly).
2. Allocate (space for) the array.
3. Initialize (the elements of the) array.
int[ ] a;
a = new int[100];
for (int i = 0; i < a.length; i++) a[ i ] = (int)(Math.random()*10);
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Just like Strings, arrays are objects.
There are three steps to setting an array up to work with it:
1. Declaration (come up with a name for the array).
At this stage the type of the elements is determined (ints, here).
(The meaning of the square brackets will be determined shortly).
2. Allocate (space for) the array.
3. Initialize (the elements of the) array.
int[ ] a;
a = new int[100];
for (int i = 0; i < a.length; i++) a[ i ] = (int)(Math.random()*10);
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Just like Strings, arrays are objects.
There are three steps to setting an array up to work with it:
1. Declaration (come up with a name for the array).
At this stage the type of the elements is determined (ints, here).
(The meaning of the square brackets will be determined shortly).
2. Allocate (space for) the array.
3. Initialize (the elements of the) array.
int[ ] a;
a = new int[100];
for (int i = 0; i < a.length; i++) a[ i ] = (int)(Math.random()*10);
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Just like Strings, arrays are objects.
There are three steps to setting an array up to work with it:
1. Declaration (come up with a name for the array).
At this stage the type of the elements is determined (ints, here).
(The meaning of the square brackets will be determined shortly).
2. Allocate (space for) the array.
3. Initialize (the elements of the) array.
int[ ] a;
a = new int[100];
for (int i = 0; i < a.length; i++) a[ i ] = (int)(Math.random()*10);
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Just like Strings, arrays are objects.
There are three steps to setting an array up to work with it:
1. Declaration (come up with a name for the array).
At this stage the type of the elements is determined (ints, here).
(The meaning of the square brackets will be determined shortly).
2. Allocate (space for) the array.
3. Initialize (the elements of the) array.
int[ ] a;
a = new int[100];
for (int i = 0; i < a.length; i++) a[ i ] = (int)(Math.random()*10);
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Just like Strings, arrays are objects.
There are three steps to setting an array up to work with it:
1. Declaration (come up with a name for the array).
At this stage the type of the elements is determined (ints, here).
(The meaning of the square brackets will be determined shortly).
2. Allocate (space for) the array.
3. Initialize (the elements of the) array.
int[ ] a;
a = new int[100];
for (int i = 0; i < a.length; i++) a[ i ] = (int)(Math.random()*10);
Arrays will remind you of Strings, in how they access their elements.
Unlike Strings, however, arrays are mutable (elements can change value).
Just like Strings, arrays are objects.
There are three steps to setting an array up to work with it:
1. Declaration (come up with a name for the array).
At this stage the type of the elements is determined (ints, here).
(The meaning of the square brackets will be determined shortly).
2. Allocate (space for) the array.
3. Initialize (the elements of the) array.
instance variable length
int[ ] a;
a = new int[100];
for (int i = 0; i < a.length; i++) a[ i ] = (int)(Math.random()*10);
So let’s look at the code alone:
So let’s look at the code alone:
int a[];
So let’s look at the code alone:
int a[];
a = new int[100];
So let’s look at the code alone:
int a[];
a = new int[100];
for (int i = 0; i < a.length; i++) {
a[i] = … ;
}
So let’s look at the code alone:
int a[];
a = new int[100];
for (int i = 0; i < a.length; i++) {
a[i] = (int)(Math.random() * 10) ;
}
How does this work?
int a[];
a = new int[100];
for (int i = 0; i < a.length; i++) {
a[i] = (int)(Math.random() * 10) ;
}
How does this work?
int a[];
a = new int[100];
for (int i = 0; i < a.length; i++) {
a[i] = (int)(Math.random() * 10) ;
}
How does this work?
int a[];
a = new int[100];
for (int i = 0; i < a.length; i++) {
a[i] = (int)(Math.random() * 10) ;
}
a
How does this work?
int a[];
a = new int[100];
for (int i = 0; i < a.length; i++) {
a[i] = (int)(Math.random() * 10) ;
}
a
What kind of variable is a?
How does this work?
int a[];
a = new int[100];
for (int i = 0; i < a.length; i++) {
a[i] = (int)(Math.random() * 10) ;
}
a
The answer is: it’s irrelevant.
It could be a local variable or an instance variable.
It could be a static variable or a parameter.
It doesn’t matter.
How does this work?
int a[];
a = new int[100];
for (int i = 0; i < a.length; i++) {
a[i] = (int)(Math.random() * 10) ;
}
a
How does this work?
int a[];
a = new int[100];
for (int i = 0; i < a.length; i++) {
a[i] = (int)(Math.random() * 10) ;
}
a
…
How does this work?
int a[];
a = new int[100];
for (int i = 0; i < a.length; i++) {
a[i] = (int)(Math.random() * 10) ;
}
a
…
0
1
2
3
4
5
6
7
98
99
How does this work?
int a[];
a = new int[100];
for (int i = 0; i < a.length; i++) {
a[i] = (int)(Math.random() * 10) ;
}
a
…
0
1
2
3
4
5
6
7
98
99
How does this work?
int a[];
a = new int[100];
for (int i = 0; i < a.length; i++) {
a[i] = (int)(Math.random() * 10) ;
}
a
…
0
i
0
1
2
3
4
5
6
7
98
99
How does this work?
int a[];
a = new int[100];
for (int i = 0; i < a.length; i++) {
a[i] = (int)(Math.random() * 10) ;
}
a
…
0
i
0
1
2
3
4
5
6
7
98
99
How does this work?
int a[];
a = new int[100];
for (int i = 0; i < a.length; i++) {
a[i] = (int)(Math.random() * 10) ;
}
a
…
0
i
0
1
2
3
4
5
6
7
98
99
100
How does this work?
int a[];
a = new int[100];
for (int i = 0; i < a.length; i++) {
a[i] = (int)(Math.random() * 10) ;
}
a
…
0
i
0
1
2
3
4
5
6
7
98
99
100
How does this work?
int a[];
a = new int[100];
for (int i = 0; i < a.length; i++) {
a[i] = (int)(Math.random() * 10) ;
}
a
…
3
0
i
0
1
2
3
4
5
6
7
98
99
100
How does this work?
int a[];
a = new int[100];
for (int i = 0; i < a.length; i++) {
a[i] = (int)(Math.random() * 10) ;
}
a
…
3
0
i
1
1
2
3
4
5
6
7
98
99
100
How does this work?
int a[];
a = new int[100];
for (int i = 0; i < a.length; i++) {
a[i] = (int)(Math.random() * 10) ;
}
a
…
3 9
0
i
1
1
2
3
4
5
6
7
98
99
100
How does this work?
int a[];
a = new int[100];
for (int i = 0; i < a.length; i++) {
a[i] = (int)(Math.random() * 10) ;
}
a
…
3 9
0
i
2
1
2
3
4
5
6
7
98
99
100
How does this work?
int a[];
a = new int[100];
for (int i = 0; i < a.length; i++) {
a[i] = (int)(Math.random() * 10) ;
}
a
…
3 9 7
0
i
2
1
2
3
4
5
6
7
98
99
100
How does this work?
int a[];
a = new int[100];
for (int i = 0; i < a.length; i++) {
a[i] = (int)(Math.random() * 10) ;
}
a
…
3 9 7
0
i
2
1
2
3
4
5
6
7
98
99
100
… and so on.
Let’s practice.
Remember the procedure of finding the largest token on a line:
Remember the procedure of finding the largest token on a line:
1 4 3 5 9 7 2
1 4 3 5 9 7 2
Which one is the biggest number?
1 4 3 5 9 7 2
1 4 3 5 9 7 2
How do you know?
1 4 3 5 9 7 2
How would you instruct the computer to find out?
1 4 3 5 9 7 2
Our program would scan the numbers left to right.
1 4 3 5 9 7 2
Our program would scan the numbers left to right.
1 4 3 5 9 7 2
We need to start somewhere.
1 4 3 5 9 7 2
We start at the beginning.
1 4 3 5 9 7 2
The first number is the largest we’ve seen.
1 4 3 5 9 7 2
The first number is the largest we’ve seen.
max
1 4 3 5 9 7 2
The first number is the largest we’ve seen.
max
1 4 3 5 9 7 2
The first number is the largest we’ve seen.
1
max
1 4 3 5 9 7 2
We move on.
1
max
1 4 3 5 9 7 2
We move on.
1
max
1 4 3 5 9 7 2
Hey, that’s a bigger number than ours!
1
max
1 4 3 5 9 7 2
Let’s take it.
1
max
1 4 3 5 9 7 2
Let’s take it.
1
max
1 4 3 5 9 7 2
Let’s take it.
4
max
1 4 3 5 9 7 2
We move on.
4
max
1 4 3 5 9 7 2
We move on.
4
max
1 4 3 5 9 7 2
This number doesn’t look interesting.
4
max
1 4 3 5 9 7 2
So we move on.
4
max
1 4 3 5 9 7 2
So we move on.
4
max
1 4 3 5 9 7 2
This number is better than ours.
4
max
1 4 3 5 9 7 2
Let’s take it.
4
max
1 4 3 5 9 7 2
Let’s take it.
4
max
1 4 3 5 9 7 2
Let’s take it.
5
max
1 4 3 5 9 7 2
And we move on.
5
max
1 4 3 5 9 7 2
We find a bigger number…
5
max
1 4 3 5 9 7 2
We find a bigger number…
9
max
1 4 3 5 9 7 2
The next number is not interesting…
9
max
1 4 3 5 9 7 2
And neither is the next one…
9
max
1 4 3 5 9 7 2
And we’re out of numbers.
9
max
1 4 3 5 9 7 2
We checked them all.
9
max
1 4 3 5 9 7 2
Each one once.
9
max
1 4 3 5 9 7 2
And this is the largest number we’ve seen.
9
max
1 4 3 5 9 7 2
So this is max.
9
max
1 4 3 5 9 7 2
9
max
Write a program that finds the largest element in an array of ints