Java Programming

Topics: Array, Shanghai Metro, Stockholm Metro Pages: 29 (5440 words) Published: February 19, 2013



Now go, write it before them in a table, and note it in a book. — Isaiah 30:8

To go beyond is as wrong as to fall short.
— Confucius

Begin at the beginning,… and go on till you come to the end: then stop. — Lewis Carroll


In this chapter you will learn:  What arrays are.  To use arrays to store data in and retrieve data from lists and tables of values.  To declare an array, initialize an array and refer to individual elements of an array.  To use the enhanced for statement to iterate through arrays.  To pass arrays to methods.  To declare and manipulate multidimensional arrays.  To write methods that use variable-length argument lists.  To read command-line arguments into a program.


6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 6.10 6.11 6.12 6.13 6.14 6.15

Introduction Arrays Declaring and Creating Arrays Examples Using Arrays Case Study: Card Shuffling and Dealing Simulation Enhanced for Statement Passing Arrays to Methods Case Study: Class GradeBook Using an Array to Store Grades Multidimensional Arrays Case Study: Class GradeBook Using a Two-Dimensional Array Variable-Length Argument Lists Using Command-Line Arguments (Optional) GUI and Graphics Case Study: Drawing Arcs (Optional) Software Engineering Case Study: Collaboration Among Objects Wrap-Up


6.1 Introduction
• Arrays
– Data structures – Related data items of same type – Remain same size once created • Fixed-length entries


6.2 Arrays
• Array
– Group of variables
• Have same type

– Reference type


Fig. 6.1 | A 12-element array.


6.2 Arrays (Cont.)
• Index
– Also called subscript – Position number in square brackets – Must be positive integer or integer expression – First element has index zero a = 5; b = 6; c[ a + b ] += 2; • Adds 2 to c[ 11 ]


Common Programming Error 6.1
Using a value of type long as an array index results in a compilation error. An index must be an int value or a value of a type that can be promoted to int—namely, byte, short or char, but not long.


6.2 Arrays (Cont.)
• Examine array c
– c is the array name – c.length accesses array c’s length – c has 12 elements ( c[0], c[1], … c[11] ) • The value of c[0] is –45


6.3 Declaring and Creating Arrays
• Declaring and Creating arrays
– Arrays are objects that occupy memory – Created dynamically with keyword new int c[] = new int[ 12 ]; – Equivalent to int c[]; // declare array variable c = new int[ 12 ]; // create array • We can create arrays of objects too String b[] = new String[ 100 ];


Common Programming Error 6.2
In an array declaration, specifying the number of elements in the square brackets of the declaration (e.g., int c[ 12 ];) is a syntax error.


Good Programming Practice 6.1
For readability, declare only one variable per declaration. Keep each declaration on a separate line, and include a comment describing the variable being declared.


Common Programming Error 6.3
Declaring multiple array variables in a single declaration can lead to subtle errors. Consider the declaration int[] a, b, c;. If a, b and c should be declared as array variables, then this declaration is correct—placing square brackets directly following the type indicates that all the identifiers in the declaration are array variables. However, if only a is intended to be an array variable, and b and c are intended to be individual int variables, then this declaration is incorrect—the declaration int a[], b, c; would achieve the desired result.


6.4 Examples Using Arrays
• Declaring arrays • Creating arrays • Initializing arrays • Manipulating array elements


6.4 Examples Using Arrays
• Creating and initializing an array
– Declare array – Create array – Initialize array elements

1 2 3 4 5 6 7 8

// Fig. 6.2: // Creating an array. public class InitArray Declare array as { array public static void main( String args[] ) of...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Java Essay
  • A Project Report On C Programming Essay
  • Essay about Concept of Java Programming
  • Java Model Test Papers for Icse
  • Essay on Structure of Programming Languages
  • History of Java Programming Essay
  • Computer Guess Paper for C++ and Java
  • The Papaer Java Test Questions and Answers Essay

Become a StudyMode Member

Sign Up - It's Free