Arrays. Eng. Mohammed Abdualal

Similar documents
Object Oriented Programming. Java-Lecture 6 - Arrays

Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department

Introduction. Data in a table or a matrix can be represented using a two-dimensional array. For example:

Array. Lecture 12. Based on Slides of Dr. Norazah Yusof

Chapter 7 Multidimensional Arrays. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Reading Input from Text File

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved

Chapter 7 Multidimensional Arrays

CS1150 Principles of Computer Science Arrays

Multidimensional Arrays. CSE 114, Computer Science 1 Stony Brook University

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Computer Programming, I. Laboratory Manual. Final Exam Solution

Chapter 8 Multi-Dimensional Arrays

Eng. Mohammed S. Abdualal

Methods. Eng. Mohammed Abdualal

Computer Programming: C++

Give one example where you might wish to use a three dimensional array

Chapter 8 Multidimensional Arrays

Computer Programming, I. Laboratory Manual. Experiment #9. Multi-Dimensional Arrays

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

Eng. Mohammed Alokshiya

Computer Programming: C++

Tutorial about Arrays

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Chapter 6 Arrays. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Chapter 6 Single-Dimensional Arrays. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

Fundamentals of Programming Data Types & Methods

Chapter 6 SINGLE-DIMENSIONAL ARRAYS

Java-Array. This tutorial introduces how to declare array variables, create arrays, and process arrays using indexed variables.

Arrays. Introduction to OOP with Java. Lecture 06: Introduction to OOP with Java - AKF Sep AbuKhleiF - 1

Chapter 6. Arrays. Java Actually: A Comprehensive Primer in Programming

Instructor: Eng.Omar Al-Nahal

LAB 12: ARRAYS (ONE DIMINSION)

Example: Monte Carlo Simulation 1

CSE 20. SAMPLE FINAL Version A Time: 180 minutes. The following precedence table is provided for your use:

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

Eng. Mohammed S. Abdualal

Programming with Java

Lab #10 Multi-dimensional Arrays

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal

Object Oriented Programming (II)

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Module 7: Arrays (Single Dimensional)

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

Data dependent execution order data dependent control flow

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Last Class. More on loops break continue A bit on arrays

CS 101 Spring 2007 Midterm 2 Name: ID:

Midterm Examination (MTA)

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

Introduction to Java & Fundamental Data Types

Last Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Chapter 9 Introduction to Arrays. Fundamentals of Java

Arrays. Weather Problem Array Declaration Accessing Elements Arrays and for Loops Array length field Quick Array Initialization Array Traversals

2.8. Decision Making: Equality and Relational Operators

CS111: PROGRAMMING LANGUAGE II

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Tasks for fmri-setting (Tasks of first and second pilot study at the end)

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

CS115 Principles of Computer Science

CS170 (005): Introduction to Computer Science Exam 2

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

CEN 414 Java Programming

Lab Session # 5 Arrays. ALQUDS University Department of Computer Engineering

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Controls Structure for Repetition

COSC 123 Computer Creativity. Java Lists and Arrays. Dr. Ramon Lawrence University of British Columbia Okanagan

Mr. Monroe s Guide to Mastering Java Syntax

Object Oriented Programming

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Arrays

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

1B1a Arrays. Arrays. Indexing. Naming arrays. Why? Using indexing. 1B1a Lecture Slides. Copyright 2003, Graham Roberts 1

CS1150 Principles of Computer Science Arrays

CS141 Programming Assignment #6

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

Announcements. PS 4 is ready, due next Thursday, 9:00pm. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

Problem Solving With Loops

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

Loops. CSE 114, Computer Science 1 Stony Brook University

Java Coding 3. Over & over again!

Warm up question. 2)Which of the following operators are defined on a half integer? Which of the following would it be useful to write as behaviors?

Over and Over Again GEEN163

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Arrays. Chapter 7 Part 3 Multi-Dimensional Arrays

Last Class. While loops Infinite loops Loop counters Iterations

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

Question: Total Points: Score:

Computer Programming, I. Laboratory Manual. Experiment #6. Loops

Supplementary Test 1

Problems with simple variables

CSC 1051 Data Structures and Algorithms I

CSE Fall 2015 Section 002 Exam 2, Time: 80 mins

Transcription:

Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2114) Created by Eng: Mohammed Alokshiya Modified by Eng: Mohammed Abdualal Lab 9 Arrays Eng. Mohammed Abdualal

November 30, 2014 An array is an ordered collection of elements of the same type, identified by a pair of square brackets [ ]. Imagine the situation that you need to store 20 names of students as strings and 20 integers as marks, then you need to define 40 variables, and this is clearly very hard and not practical, in such case you need to use arrays. Arrays are indexed data types, that means they are storing different elements discriminating between them using unique index for each one. Figure (1) shows the structure of an array. Figure (1) As Figure (1) shows, the size of an array is fixed, we will refer to array maximum size as array length, it is also clear that indices of an array are zero-based, that is, they start from 0 to length 1. The syntax of declaring Array in Java is as follow: Array s Declaration Syntax DataType [] arrayname; To initialize the array, use new keyword and specify the length. Initializing an Array DataType [] arrayname = new DataType[arrayLength]; Examples: Examples int [] marks = new int[5]; // array of 5 integers double [] values = new double[12]; // array of 12 doubles boolean [] primes = new boolean[2]; // array of 2 booleans String [] names = new String[50]; // array of 50 strings 2

When you initialize an array using new keyword, all elements will take the default value of the array data type. For example, all elements of marks array will be 0, all values of primes array will be false, and all values of names array will be null. Note that you can write the square brackets after the array name, like this: Array s Declaration Syntax int marks[]; However, this is not a recommended syntax. To access array elements, use array name with desired index specified between square brackets [ ]. Accessing Array Elements int [] array = new int[5]; array[0] = 0; array[1] = 1; array[2] = 4; array[3] = 9; array[4] = 16; You can use loops to access array elements, for example printing them to the console. Printing Array Elements for (int i = 0; i < 5; i++) { System.out.println(array[i]); Array Initializers Java has a shorthand notation, known as the array initializer, which combines the declaration, creation, and initialization of an array in one statement using the following syntax: Array s Initializer Syntax elementtype[] arrayrefvar = {value0, value1,..., valuek; Example: Array Initializers double[] mylist = {1.9, 2.9, 3.4, 3.5; 3

The previous statement declares, creates, and initializes the array mylist with four elements, which is equivalent to the following statements: Equivalent Statements double[] mylist = new double[4]; mylist[0] = 1.9; mylist[1] = 2.9; mylist[2] = 3.4; mylist[3] = 3.5; Example: write a program to read 10 integers, and print them in reverse order. Code import java.util.scanner; public class Main { public static void main(string[] args) { Scanner input = new Scanner(System.in); int [] arr = new int[10]; for (int i = 0; i < 10; i++) { arr[i] = input.nextint(); for (int i = 9; i >= 0; i--) { System.out.println(arr[i]); Passing Arrays to Methods When passing an array to a method, the reference of the array is passed to the method. Example: write a method sum that take an array of integers as input, and return the sum of all integers in it. sum Method public static int sum (int [] array) { int s = 0; for (int i = 0; i < array.length; i++) { s += array[i]; return s; 4

You call the previous method by passing the array name into it: Calling sum Method public static void main(string[] args) { int [] arr = {1, 2, 3, 4, 5; int s = sum(arr); System.out.println(s); Multi-Dimensional Array Multi-Dimensional array is an array of arrays. For example, to declare two-dimensional array, use the following syntax: Two-Dimensional Array Declaration Syntax DataType [][] matrix; To initialize the matrix, use the new keyword. Example: create a new matrix with 5 rows and 5 columns: Initializing Two-Dimensional Array int [][] matrix = new int[5][5]; The previous code will create the matrix and fill it with the default value: 0. The matrix will be represented in memory as array of arrays, as following: 5

For example, suppose x = new int[3][4], x[0], x[1], and x[2] are one-dimensional arrays and each contains four elements, as shown in the next figure. x.length is 3, and x[0].length, x[1].length, and x[2].length are 4. 6