1989 02 Supergain Antennas and the Yagi and Circular Arrays.pdf
CSE 2221 - Arraysweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/08.Arrays.pdf · Array...
Transcript of CSE 2221 - Arraysweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/08.Arrays.pdf · Array...
![Page 1: CSE 2221 - Arraysweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/08.Arrays.pdf · Array • An . array . is a group of similar variables, all of the same type, and with](https://reader030.fdocuments.us/reader030/viewer/2022040403/5e88f68e5156c52c3460dd51/html5/thumbnails/1.jpg)
Arrays
7 January 2019 OSU CSE 1
![Page 2: CSE 2221 - Arraysweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/08.Arrays.pdf · Array • An . array . is a group of similar variables, all of the same type, and with](https://reader030.fdocuments.us/reader030/viewer/2022040403/5e88f68e5156c52c3460dd51/html5/thumbnails/2.jpg)
Array• An array is a group of similar variables, all
of the same type, and with systematically related names that involve special syntax using […]
• Each array element, e.g., a[0], a[1], …, acts like a single variable of the type used in the declaration of array a
7 January 2019 OSU CSE 2
![Page 3: CSE 2221 - Arraysweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/08.Arrays.pdf · Array • An . array . is a group of similar variables, all of the same type, and with](https://reader030.fdocuments.us/reader030/viewer/2022040403/5e88f68e5156c52c3460dd51/html5/thumbnails/3.jpg)
Compare to Mathematics
• This is entirely parallel to the use of subscripted variables in mathematics, e.g., x0, x1, …
• Just as x0 is pronounced “x-sub-0” in mathematics, a[0] is usually pronounced “a-sub-0” in a Java program
• Consider, similarly, xi+2 and a[i+2]
7 January 2019 OSU CSE 3
![Page 4: CSE 2221 - Arraysweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/08.Arrays.pdf · Array • An . array . is a group of similar variables, all of the same type, and with](https://reader030.fdocuments.us/reader030/viewer/2022040403/5e88f68e5156c52c3460dd51/html5/thumbnails/4.jpg)
Compare to Mathematics
• In mathematics, a group of related variables x0, x1, …, xn-1 is called a vector x of length n
• In Java, a group of variables a[0], a[1], …, a[n-1] is called an array a of length n
7 January 2019 OSU CSE 4
![Page 5: CSE 2221 - Arraysweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/08.Arrays.pdf · Array • An . array . is a group of similar variables, all of the same type, and with](https://reader030.fdocuments.us/reader030/viewer/2022040403/5e88f68e5156c52c3460dd51/html5/thumbnails/5.jpg)
Declaring an Array int[] a;
7 January 2019 OSU CSE 5
![Page 6: CSE 2221 - Arraysweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/08.Arrays.pdf · Array • An . array . is a group of similar variables, all of the same type, and with](https://reader030.fdocuments.us/reader030/viewer/2022040403/5e88f68e5156c52c3460dd51/html5/thumbnails/6.jpg)
Declaring an Array int[] a;
7 January 2019 OSU CSE 6
The [] in this declaration indicates there will be some number of variables named
a[0], a[1], …But, how many?
![Page 7: CSE 2221 - Arraysweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/08.Arrays.pdf · Array • An . array . is a group of similar variables, all of the same type, and with](https://reader030.fdocuments.us/reader030/viewer/2022040403/5e88f68e5156c52c3460dd51/html5/thumbnails/7.jpg)
Declaring and Creating an Array int[] a = new int[4];
7 January 2019 OSU CSE 7
![Page 8: CSE 2221 - Arraysweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/08.Arrays.pdf · Array • An . array . is a group of similar variables, all of the same type, and with](https://reader030.fdocuments.us/reader030/viewer/2022040403/5e88f68e5156c52c3460dd51/html5/thumbnails/8.jpg)
Declaring and Creating an Array int[] a = new int[4];
7 January 2019 OSU CSE 8
This many! Here, 4 is called the length of the array, and it is the
value of another variable introduced by this declaration:
a.length
![Page 9: CSE 2221 - Arraysweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/08.Arrays.pdf · Array • An . array . is a group of similar variables, all of the same type, and with](https://reader030.fdocuments.us/reader030/viewer/2022040403/5e88f68e5156c52c3460dd51/html5/thumbnails/9.jpg)
Declaring and Creating an Arrayint[] a = new int[4];
7 January 2019 OSU CSE 9
0 0 0 0
4
![Page 10: CSE 2221 - Arraysweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/08.Arrays.pdf · Array • An . array . is a group of similar variables, all of the same type, and with](https://reader030.fdocuments.us/reader030/viewer/2022040403/5e88f68e5156c52c3460dd51/html5/thumbnails/10.jpg)
Understanding Arraysint[] a = new int[4];
7 January 2019 OSU CSE 10
![Page 11: CSE 2221 - Arraysweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/08.Arrays.pdf · Array • An . array . is a group of similar variables, all of the same type, and with](https://reader030.fdocuments.us/reader030/viewer/2022040403/5e88f68e5156c52c3460dd51/html5/thumbnails/11.jpg)
Understanding Arraysint[] a = new int[4];
7 January 2019 OSU CSE 11
int a[0] = 0;int a[1] = 0;int a[2] = 0;int a[3] = 0;int a.length = 4;
This is illegal Java code, but it is the net
effect of the array declaration/creation
above.
![Page 12: CSE 2221 - Arraysweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/08.Arrays.pdf · Array • An . array . is a group of similar variables, all of the same type, and with](https://reader030.fdocuments.us/reader030/viewer/2022040403/5e88f68e5156c52c3460dd51/html5/thumbnails/12.jpg)
Declaring and Initializing an Array int[] a = { 6, 18, 9, –10 };
7 January 2019 OSU CSE 12
![Page 13: CSE 2221 - Arraysweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/08.Arrays.pdf · Array • An . array . is a group of similar variables, all of the same type, and with](https://reader030.fdocuments.us/reader030/viewer/2022040403/5e88f68e5156c52c3460dd51/html5/thumbnails/13.jpg)
Declaring and Initializing an Array int[] a = { 6, 18, 9, –10 };
7 January 2019 OSU CSE 13
Here again, we have:a.length = 4
But now the 4 array elements have different initial values:
a[0] = 6a[1] = 18
etc.
![Page 14: CSE 2221 - Arraysweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/08.Arrays.pdf · Array • An . array . is a group of similar variables, all of the same type, and with](https://reader030.fdocuments.us/reader030/viewer/2022040403/5e88f68e5156c52c3460dd51/html5/thumbnails/14.jpg)
int[] a = { 6, 18, 9, –10 };
7 January 2019 OSU CSE 14
Declaring and Initializing an Array
6 18 9 -10
4
![Page 15: CSE 2221 - Arraysweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/08.Arrays.pdf · Array • An . array . is a group of similar variables, all of the same type, and with](https://reader030.fdocuments.us/reader030/viewer/2022040403/5e88f68e5156c52c3460dd51/html5/thumbnails/15.jpg)
Understanding Arraysint[] a = { 6, 18, 9, -10 };
7 January 2019 OSU CSE 15
![Page 16: CSE 2221 - Arraysweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/08.Arrays.pdf · Array • An . array . is a group of similar variables, all of the same type, and with](https://reader030.fdocuments.us/reader030/viewer/2022040403/5e88f68e5156c52c3460dd51/html5/thumbnails/16.jpg)
Understanding Arraysint[] a = { 6, 18, 9, -10 };
7 January 2019 OSU CSE 16
int a[0] = 6;int a[1] = 18;int a[2] = 9;int a[3] = -10;int a.length = 4;
This is illegal Java code, but it is the net
effect of the array declaration/initializati
on above.
![Page 17: CSE 2221 - Arraysweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/08.Arrays.pdf · Array • An . array . is a group of similar variables, all of the same type, and with](https://reader030.fdocuments.us/reader030/viewer/2022040403/5e88f68e5156c52c3460dd51/html5/thumbnails/17.jpg)
Array Indexing with Constants
• You may write an int constant (literal) cbetween […] as in a[c], so long as its value satisfies:0 ≤ c < a.length
• Example:int[] a = new int[4];a[3] = 17;
7 January 2019 OSU CSE 17
![Page 18: CSE 2221 - Arraysweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/08.Arrays.pdf · Array • An . array . is a group of similar variables, all of the same type, and with](https://reader030.fdocuments.us/reader030/viewer/2022040403/5e88f68e5156c52c3460dd51/html5/thumbnails/18.jpg)
Array Indexing with Constants
• You may write an int constant (literal) cbetween […] as in a[c], so long as its value satisfies:0 ≤ c < a.length
• Example:int[] a = new int[4];a[3] = 17;
7 January 2019 OSU CSE 18
After this code is executed, we have
a[3] = 17
![Page 19: CSE 2221 - Arraysweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/08.Arrays.pdf · Array • An . array . is a group of similar variables, all of the same type, and with](https://reader030.fdocuments.us/reader030/viewer/2022040403/5e88f68e5156c52c3460dd51/html5/thumbnails/19.jpg)
Array Indexing in General
• You may write an int-valued expression exp between […] as in a[exp], so long as its value satisfies:0 ≤ exp < a.length
• Example:int[] a = new int[4];a[a.length – 1] = 17;
7 January 2019 OSU CSE 19
![Page 20: CSE 2221 - Arraysweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/08.Arrays.pdf · Array • An . array . is a group of similar variables, all of the same type, and with](https://reader030.fdocuments.us/reader030/viewer/2022040403/5e88f68e5156c52c3460dd51/html5/thumbnails/20.jpg)
Array Indexing in General
• You may write an int-valued expression exp between […] as in a[exp], so long as its value satisfies:0 ≤ exp < a.length
• Example:int[] a = new int[4];a[a.length – 1] = 17;
7 January 2019 OSU CSE 20
After this code is executed, we have:
a[3] = 17
![Page 21: CSE 2221 - Arraysweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/08.Arrays.pdf · Array • An . array . is a group of similar variables, all of the same type, and with](https://reader030.fdocuments.us/reader030/viewer/2022040403/5e88f68e5156c52c3460dd51/html5/thumbnails/21.jpg)
Resources
• Java Tutorials– http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
• Java for Everyone, Chapter 6– http://osu.worldcat.org/title/java-for-everyone-late-objects/oclc/808511232
7 January 2019 OSU CSE 21