Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Arrays: Fixed-Sized Collections of Variables - Prof. Brian F. Hanks, Study notes of Javascript programming

An introduction to arrays in java, a fixed-sized collection of variables of the same type with a single identifier. Topics covered include array declaration, initialization, accessing elements, assigning values, and using arrays with loops. Examples are given for numeric, boolean, and reference types.

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-hn4-1
koofers-user-hn4-1 🇺🇸

10 documents

1 / 5

Toggle sidebar

Related documents


Partial preview of the text

Download Java Arrays: Fixed-Sized Collections of Variables - Prof. Brian F. Hanks and more Study notes Javascript programming in PDF only on Docsity! CSIS 110 - Lecture 15 Announcements Read Section 4.12 again! Next Program on course web site! Quiz Review Arrays A few days ago I said that Java provided two ways of keeping a set of values in a collection: 1) Collection classes in the Java library. 2) Arrays. We're going to look at arrays now. Arrays An array is a fixed-sized set of variables of the same type that have a single identifier associated with them. Each of the variables is called an element of the array. An array is similar to an ArrayList, but: - included as part of language, not as a library class o special syntax o more efficient - fixed size - Elements of ArrayList must be objects. Arrays can have elements of any type, including int, double, char, boolean Declaration of an array variable: <ElementType>[] <identifier>; Examples: int[] quizScores; double[] temperature; String[] monthNames; int[] daysPerMonth; Student[] classList; These declare arrays whose elements are the given type. The type of these variables is “array of <type>”. Arrays are objects, so these declarations only declare/allocate the reference. We need to use new to create the array elements themselves. quizScores = new int[ 18 ]; temperature = new double[ 365 ]; monthNames = new String[ 12 ]; daysPerMonth = new String[ 12 ]; classList = new Student[ numStudents ]; Draw picture of one or two of these. Show what happens to the array elements. numeric types initialized to 0 Booleans initialized to false References initialized to null – means that they don’t reference anything yet. Can also do both parts in a single declaration Emphasize that declaring the array only creates the reference. You have to allocate the array elements also! Accessing array elements Ok, this is all great. I've created these arrays that are sets of variables with the same name. How do I actually use the array elements in a program? We access the array elements using subscripting or indexing. Each array element has a subscript (or index) that we can use to refer to that element. The subscripts are integer values, starting at 0. Thus, the first element of the array has subscript 0, the second has subscript 1, and the last has a subscript of the array-size - 1. [Note similarity to indexing in ArrayList]. int[] daysPerMonth = new int[12]; Draw picture, showing the array subscripts, and the names of each element (daysPerMonth[0], daysPerMonth[1], ...) The elements of an array are referenced using the array name and a subscript. So, we can say daysPerMonth[6] or quizScores[4] or temperature[300]. The value of the index or subscript MUST BE an integer expression. That means we don't have to use constants. temperature[ index ] daysPerMonth[ currentMonth ] Assigning values to array elements Use an assignment statement, but the variable on the left-hand side is indexed. monthName[0] = "January"; daysPerMonth[0] = 31; // January (1 - 1)
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved