Preview

Java Programming

Powerful Essays
Open Document
Open Document
5440 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Java Programming
1

6
Arrays

2

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

3

OBJECTIVES
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.

4

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

5

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

6

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

– Reference type

7

Fig. 6.1 | A 12-element array.

8

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 ]

9

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

You May Also Find These Documents Helpful

  • Good Essays

    Nt1420 Unit 6

    • 1145 Words
    • 5 Pages

    INSTRUCTIONS: 1. THERE ARE SIX (6) QUESTIONS IN THIS PAPER. 2. ANSWER FIVE (5) QUESTIONS ONLY. Question 1 Arrays are used when storing a large number of values. You are required to create an array named a and answer the following questions regarding array manipulation. a. Write a method fillRandom(int[] a, int min, int max), fill the array a with a random integer value. (Note: Math.random() returns a double in the range of 0.0 and 1.0, therefore it is cast to an integer number, between the minimum and maximum value). [6 marks] b. Write the Bubble sort method to sort array a into descending order. [10 marks] c. In the quicksort, an algorithm an element is chosen from the unsorted list. This element is called the…

    • 1145 Words
    • 5 Pages
    Good Essays
  • Satisfactory Essays

    IT215 INVENTORY PART DVD

    • 302 Words
    • 2 Pages

    Week Six introduces superclasses and subclasses and their relationship to inheritance. These subjects are built upon to introduce the concept of polymorphism. Students learn how to declare, create, initialize, and manipulate arrays.…

    • 302 Words
    • 2 Pages
    Satisfactory Essays
  • Powerful Essays

    The purpose of this Lab. is to familiarize student how to solve practical problems programmatically; they will practice on elementary programming using primitive data types, variables, constants, operators, expressions, and input and output. Also, they will learn how to diagnose errors that may occur when a program is compiled or executed. There are some exercises, through which they will understand the concept learn in this chapter.…

    • 2338 Words
    • 10 Pages
    Powerful Essays
  • Good Essays

    Arrays store items that have the same type of data type like a group of employees’ names and social security numbers for a team of 2000 personal. Pointer is a variable that greatly extends the power and flexibility of a program, each memory location that is used to store data value has an address. The address provides the means for a PC hardware to reference a particular data item.…

    • 485 Words
    • 2 Pages
    Good Essays
  • Good Essays

    Array Structure Paper

    • 833 Words
    • 4 Pages

    “An array is a collective name given to a group of similar quantities. These similar quantities could be percentage marks of 100 students, number of chairs in home, or salaries of 300 employees or ages of 25 students. Thus an array is a collection of similar elements. These similar elements could be all integers or all characters, and so on” (Thompson, 2007). Building an array can be confusing for the Bug Blasters can be very confusing for a programmer who does not fully understand how they work.…

    • 833 Words
    • 4 Pages
    Good Essays
  • Powerful Essays

    Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Chapter 15 Chapter 16 Chapter 17 Chapter 18 Chapter 19 Chapter 20 Chapter 21 Chapter 22 Chapter 23 Chapter 24 Chapter 25 Chapter 26 Chapter 27 An Introduction to Hardware, Software, and the Internet An Introduction to Software Development Objects and Classes Algorithms Java Syntax and Style Data Types, Variables, and Arithmetic Boolean Expressions and if-else Statements Iterative Statements: while, for, do–while Implementing Classes and Using Objects Strings Class Hierarchies and Interfaces Arrays…

    • 3908 Words
    • 16 Pages
    Powerful Essays
  • Good Essays

    7 and Array

    • 500 Words
    • 2 Pages

    7. When a single array element, such as myArray[2], is passed to a method, the method receives _____.…

    • 500 Words
    • 2 Pages
    Good Essays
  • Satisfactory Essays

    This course introduces students to object-oriented programming. It covers object-oriented tools for system analysis, design and development. The course teaches the significance of object-oriented programming, the keywords and constructs of the Java programming language, and the steps required to create simple Java technology programs.…

    • 414 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    References: Deitel, H.M., & Deitel, P.J. (2002). Java: How to program (6th ed.). Upper Saddle River, NJ: Pearson Education.…

    • 253 Words
    • 2 Pages
    Satisfactory Essays
  • Good Essays

    Java

    • 490 Words
    • 3 Pages

    Write a Java program to demonstrate using bitmaps and bitwise operators to sort and remove duplicates from a file of random phone numbers. Do not confuse the term bitmap used for compressing data into smaller spaces with the bitmap that has come to mean a graphic image.…

    • 490 Words
    • 3 Pages
    Good Essays
  • Good Essays

    It210 Syllabus

    • 2333 Words
    • 10 Pages

    Objectives 1.1 Describe the importance of using a structured, modular approach when creating program requirements, design, and code. 1.2 Identify how a computer processes and stores data. Read the course description and objectives. Read the instructor’s biography and post your own. Read Appendix A. Read Ch. 2 of Prelude to Programming. Read Ch. 3 of Prelude to Programming.…

    • 2333 Words
    • 10 Pages
    Good Essays
  • Satisfactory Essays

    1 12. 2 13. 1 14. 2 15. 1 16. 2 17. 1 18.…

    • 2040 Words
    • 9 Pages
    Satisfactory Essays
  • Good Essays

    References: Deitel, H.M., & Deitel, P.J. (2002). Java: How to program (6th ed.). Upper Saddle River, NJ: Pearson Education…

    • 356 Words
    • 2 Pages
    Good Essays
  • Better Essays

    Data Type and Array

    • 760 Words
    • 4 Pages

    * datatype: Is used to specify the data type for the elements, which will be stored in the array.…

    • 760 Words
    • 4 Pages
    Better Essays
  • Good Essays

    java introduction

    • 1904 Words
    • 8 Pages

    Java was conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991.It took 18 months to develop the first working version. This language was initially called “Oak” but was renamed “Java” in 1995. Original impetus for Java was not the Internet! Instead, the primary motivation was the need for a platform-independent (that is, architectureneutral) language that could be used to create software to be embedded in various consumer electronic devices, such as microwave ovens and remote controls. As you can probably guess, many different types of CPUs are used as controllers. The trouble with C and C++ (and most other languages) is that they are designed to be compiled…

    • 1904 Words
    • 8 Pages
    Good Essays