Docsity

Arrays and Vectors - Java Programming Language - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Arrays and Vectors - Java Programming Language - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Chapter 7 Arrays and Vectors

• • Introducing ArraysDeclaring Arrays, Creating Arrays, and

• Initializing ArraysArray of Objects

• • Copying ArraysMultidimensional Arrays

• • Numeric Wrapper ClassesCommand-Line Parameters

• • Creating Generic ClassesThe Vector Class

Introducing Arrays

myList[0] myList[1]

myList[2] myList[3]

myList[4] myList[5]

myList[6] myList[7]

double[] myList = new double[10] myList[8] myList[9]

Array is a data structurethat represents a

collection of the sametypes of data. Java treats

these arrays as objects.

An Array of 10 Elementsof type double

Creating Arrays

arrayName = new datatype[arraySize];

Example: myList = new double[10];

Declaring and Creating

in One Step

• datatype[] arrayname = new datatype[arraySize];

double[] myList = new double[10];

• datatype arrayname[] = newdatatype[arraySize];

double myList[] = new double[10];

Example 7.

Assigning Grades

• Objective:keyboard, get the best score, and then assign read student scores (int) from the

grades based on the following scheme: – Grade is A if score is >= best–10;

– – Grade is B if score is >= best–20;Grade is C if score is >= best–30;

– – Grade is D if score is >= best–40;Grade is F otherwise.

AssignGrade Run

Example 7.

Using Arrays in Sorting

• Objective: Use thewrite a program that will sort a list of selectionSort method to

double floating-point numbers.

SelectionSort Run

Array of Objects

• Declaring and creating: Circle[] circleArray = new Circle[10];

• Initializing: for (int i=0; i<circleArray.length; i++)

{ circleArray[i] = new Circle();

Summarizing the Areas of CirclesExample 7.

• Objective:array of circles Summarize the areas of an

TotalArea Run

The arraycopy Utility

arraycopy(sourceArray, src_pos, targetArray,tar_pos, length);

Example: System.arraycopy(sourceArray, 0, targetArray, 0,

sourceArray.length);

Multidimensional Arrays

int[][] matrix = new int[10][10]; or

int matrix[][] = new int[10][10]; for (int i=0; i<matrix.length; i++) for (int j=0; j<matrix[i].length; j++) { }^ matrix[i][j] = (int)(Math.random()*1000);

Numeric Wrapper Classes

  • • BooleanCharacter
  • • ShortByte

Object

Double Float Long Integer Short Byte

Number Character Boolean

LongFloat Double

The Integer Class

and The Double Class

• Constructors

• Class Constants MAX_VALUE , MIN_VALUE

• Conversion Methods

Processing

Command-Line Parameters

In the main method, get the arguments fromargs[0], args[1], ..., args[n] , which corresponds

to arg0, arg1, ..., argn in the command line.

Using Command-LineExample 7.

Parameters

• Objective: Write a program that willperform binary operations on integers.

The program receives three parameters:an operator and two integers.

Run java Calculator + 2 3^ Calculator Run java Calculator - 2 3 Run java Calculator / 2 3 Docsity.com