Chapter 6 SINGLE-DIMENSIONAL ARRAYS

Size: px
Start display at page:

Download "Chapter 6 SINGLE-DIMENSIONAL ARRAYS"

Transcription

1 Chapter 6 SINGLE-DIMENSIONAL ARRAYS Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk

2 What is an Array? A single array variable can reference a large collection of data. Arrays have three important properties: arrays represent a group of related data (for example, temperature for the last five days, or stock prices for the last 30 days.) all data within a single array must share the same data type (for example, you can create an array of ints or an array of floats, but you cannot mix and match ints with floats.) The size of an array is fixed once it is created.

3 What exactly are we talking about?! Let s say you have ten students and you want to save the grades for each of them for use throughout your program (i.e. we need to remember every grade not just loop and count them or average them, etc.) Could do it the hard way: Set up ten variables called studentonegrade, studenttwograde, studentthreegrade, etc. Very difficult to use and manipulate Instead, could use an array to do it the easy way

4 Using an array variable Create an array variable called studentgrades[10] Declared as follows: int studentgrades[] = new int[10]; This sets up a location in memory for 10 integers which can be referenced using studentgrades[ [ # ] where # is the particular student you want to look at.

5 Array Naming Considerations The rules for naming variables apply when selecting array variable names Composed of letters, digits, dollar signs and underscore characters Cannot start with a digit Follow all the good programming hints recommended for variable names i.e. just think of it as an ordinary variable when making up the name In addition it is bad style to end an array name with a digit ie. array3[5]

6 Parts of the array The array has some terminology we haven t seen yet Elements Index Position Number Subscript Zeroth element Keyword new

7 Elements Refers to the individual items represented by the array. For example, an array of 10 integers is said to have 10 elements an array of 15 floats has 15 elements an array of 5 characters has 5 elements and so on

8 Index (Position Number or Subscript) Refers to one particular element in the array Also known as position number or, more formally, as a subscript The first element in an array is represented by an index or subscript of 0 (zero). For example, studentgrades[ [ 0 ] This first position is known as the zeroth element The second position is referred to by studentgrades[ [ 1 ]

9 Figuring out the array positions In Java, an arrays elements start out at index 0 and go up to (the number of elements 1) For example, our array of 10 student grades filled in with grades might look like: Index Value

10 Array positions (cont d) We can access them by specifying the array name followed by square brackets with the index number between them. For example, System.out.println ("The third student s s grade is " + studentgrades[ [ 2 ] ); Would print only the third integer spot in the array (remember 0 is the first, 1 is the second, and 2 is the third element s index / subscript) The output would look like the following: The third student s grade is 99

11 Array positions (cont d) The index scheme starting at 0 may be initially confusing. This is the cause of many off-by-one errors so study the concept carefully The element in the array s first position is sometimes referred to as the zeroth element. Notice the difference between "position" and "element number"

12 subscript terminology From this point forward, for this class, all references to array elements will refer to the subscript of the array. In the event that we want to discuss the position of an element in an array, we will refer to it s position. In other words, we will not use i'th element or element i notation unless we are referring to the zeroth element.

13 Keyword new As we will see, the keyword new is used in Java when you wish to create a new object. In Java, arrays are objects. As with all data members of objects, the elements of the each position in a new array will automatically be initialized to the default value for the array s type.

14 Some powerful features of arrays Can use expressions as the subscript E.g. if variables a = 1 and b = 2 studentgrades[ [ a + b ] would be the same as writing studentgrades[ [ 3 ] Can use array elements in expressions E.g. int gradetotal = 0 ; gradetotal = studentgrades[ [ 0 ] + studentgrades[ [ 1 ] + studentgrades[ [ 2 ] + etc studentgrades[ [ 9 ] ; Would add up all the array elements and store them in gradetotal.

15 Powerful features (cont d) Can set array elements equal to other values or expressions. For example, studentgrades[ [ 1 ] = 100 ; This would set the array element with a subscript of 1 to a grade of 100. That is, the second student would have a grade of 100. Java allows us to access an array s length by using the following notation: studentgrades.length would evaluate to 10

16 So how do we use arrays? Same concepts as other variables apply Must declare the array Must initialize the array (unlike regular variables, Java will automatically initialize arrays with default values) Arrays variables are actually reference variables. Can use arrays in expressions and methods, setting elements values or using their values, similar to the use of ordinary variables

17 Declaring an array First you can declare an array reference variable (you must specify the type of the elements in the array) : int [] myfirstarray; //declares an array //variable for ints Note: the line above does not allocate memory for the array (it cannot since we have not said the size of the array yet). It only sets aside enough memory to reference the array. Until we create an array, the reference is to null. Next, we create the array and point the reference to it: myfirstarray = new int [10]; To create the array, we need the number of elements in the array so the computer can set aside adequate memory for the array. We can combine the declaration and allocation lines into one line as follows: int [] myfirstarray = new int [10];

18 Initializing array with a for loop After declaring an array, you can initialize it in the body of your program by using a for loop: int [] myfirstarray = new int [ 5 ]; for (int( i = 0 ; i <= 4 ; i++ ) { myfirstarray[ [ i ] = 1 ; } // end for Note the upper bound is 4, not 5! That is, you loop through 0 to 4 to initialize an array with 5 elements Note: Array elements are initialized with default values. Numeric types get 0, boolean get false and char gets the null character (ascii code 0).

19 Declaring arrays (cont) You can use a constant to set the size of the array final int NUM_STUDENTS_IN_CLASS = 8 int [] exams = new int [NUM_STUDENTS_IN_CLASS ]; In Java, you do not need to know the size of the array at COMPILE time. Instead you can know the size at RUN time. For example, the following is legal: inputstring = JOptionPane.showInputDialog ("How many students?"); int students = Integer.parseInt (inputstring); int [] myfirstarray = new int [students];

20 Initializing an Array You can initialize an array when you declare it, as you do with other variables Syntax is slightly different, as you are now initializing more than one element at a time One way at declaration, using initializers: int myfirstarray[ [ ] = { 0, 0, 0, 0, 0 }; Note the braces around the initial zeroes which themselves are separated by commas. Also note that creating arrays in this way eliminates the need for the keyword new.

21 Accessing elements with for loop Can use a for loop to print the contents of an array int [] myfirstarray = new int [ 5 ]; for (int( i = 0 ; i <= myfirstarray.length 1; i++ ) { System.out.println ("array element " + i + " is equal to " + myfirstarray [i]); } // end for For an array of the char[ ] type, it can be printed using one print statement. For example, the following code displays IYAD : char[ ] name = { I {, Y, A, D }; System.out.println(name);

22 for-each Loops Outline In general, the syntax for a for-each loop is for (elementtype( element: arrayrefvar) ) { } // Process the element For example, the following code displays all the elements in the array mylist : for(double u: mylist) { System.out out.println(u); } 2003 Prentice Hall, Inc. All rights reserved.

23 Array Bounds Very Important: Java does not provide array bounds checking at compile time. This means that your program will crash at run time of you try to access memory outside of an array s bounds. For example, suppose you have: int [] myfirstarray = new int [ 10 ]; myfirstarray[100] = 45; 100 is very far outside your defined size (0..9) However, the compiler will not indicate an error.

24 Outline Example Using Arrays Using histograms do display array data graphically Histogram Plot each numeric value as bar of asterisks (*) 2003 Prentice Hall, Inc. All rights reserved.

25 1 // Histogram.java 2 // Histogram printing program. 3 import javax.swing.*;.*; 4 5 public class Histogram { 6 Declare array with initializer list 7 public static void main( String args[] ) 8 { 9 int array[] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 }; String output = "Element\tValue tvalue\thistogram thistogram"; // for each array element, output a bar in histogram 14 for ( int counter = 0; counter < array.length; counter++ ) { 15 output += "\n" + counter + "\t" + array[ counter ] + "\t" t"; // print bar of asterisks 18 for ( int stars = 0; stars < array[ counter ]; stars++ ) 19 output += "*"; } // end outer for JTextArea outputarea = new JTextArea(); 24 outputarea.settext( output ); 25 For each array element, print associated number of asterisks Outline Histogram.java Line 9 Declare array with initializer list Line 19 For each array element, print associated number of asterisks 2003 Prentice Hall, Inc. All rights reserved.

26 26 JOptionPane.showMessageDialog( null, outputarea, 27 "Histogram Printing Program", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } // end main } // end class Histogram Outline Histogram.java 2003 Prentice Hall, Inc. All rights reserved.

27 References and Reference Parameters Outline Two ways to pass arguments to methods Pass-by-value Copy of argument s value is passed to called method In Java, every primitive is pass-by-value Pass-by-reference Caller gives called method direct access to caller s data Called method can manipulate this data Improved performance over pass-by-value In Java, every object is simulated pass-by-reference In Java, arrays are objects Therefore, arrays are passed to methods by reference Technically, we are using pass by value but the value we are passing is a reference to the array Prentice Hall, Inc. All rights reserved.

28 Passing Arrays to Methods Outline To pass array argument to a method Specify array name without brackets Array hourlytemperatures is declared as int [ ] mylist ={1,2,3,4}; The method call printarray( mylist ); Or printarray( ( new int[]{1,2,3,4} ); 2003 Prentice Hall, Inc. All rights reserved.

29 Passing arrays to methods (cont) In the method header, use similar syntax as that for array declaration: public static void printarray (int array [] ) or public static void ptintarray (int [] array ) For example, the following method displays the elements in an int array: Public static void printarray(int [] array){ for(int i=0 ; i < array.length ; ++i) system.out.println( array[i] ] + ); }

30 Example for pass by sharing Outline Public class Test{ } } public static void main (String [ ] args){ int x=1; int [ ] y = new int [10]; m( x, y ); System.out.println( x is + x); System.out.println( y[0] y[0] is + y[0] ); public static void m( int number, int [ ] numbers){ } number = 1001; numbers[0] = 5555; X is 1 Y[0] is Prentice Hall, Inc. All rights reserved.

31 Outline Example for pass by sharing, cont. The primitive type value in x is passed to number, and the reference value in y is passed to numbers Prentice Hall, Inc. All rights reserved.

32 Returning arrays from methods We have seen examples of methods that modify an array. Since the array is passed by reference, it is modified in the original method as well. Another way to return information in an array is to explicitly return the array. For example, a method with the header: public static int [] makearray () Returns an integer array.

33 Returning arrays (cont) In order to return an array, the method creates the array first. public static int [] makearray () { int [] myarray = new int [10]; for (int( i; i < myarray.length; i++) Myarray [i] = i; return myarray; } The method above creates a new array called myarray, initializes each element and returns the reference to myarray.

34 Returning arrays (cont) Outline For example, the following method returns an array that is the reversal of another array. For example, the following statement returns a new array list2 with elements 6,5,4,3,2,1 Int [ ] list1={1,2,3,4,5,6}; Int [ ] list2= reverse(list1); 2003 Prentice Hall, Inc. All rights reserved.

35 Outline Variable-Length Argument Lists A variable number of arguments of the same type can be passed to a method and treated as an array. You can pass a variable number of arguments of the same type to a method. The parameter in the method is declared as follows: typename... parametername Example: 2003 Prentice Hall, Inc. All rights reserved.

36 Example of Copying Arrays In this example, you will see that a simple assignment cannot copy arrays in the following program. The program simply creates two arrays and attempts to copy one to the other, using an assignment statement.

37 Copying Arrays Before the assignment list2 = list1; After the assignment list2 = list1; list1 Contents of list1 list1 Contents of list1 list2 Contents of list2 list2 Garbage Contents of list2

38 Copying Arrays Using a loop: int[] sourcearray = {2, 3, 1, 5, 10}; int[] targetarray = new int[sourcearray.length]; for (int( i = 0; i < sourcearrays.length; ; i++) targetarray[i] ] = sourcearray[i];

39 The arraycopy Utility arraycopy(sourcearray, src_pos, targetarray, tar_pos,, length); Example: System.arraycopy(sourceArray,, 0, targetarray, 0, sourcearray.length);

40 The Arrays Class Outline The java.util.arrays class contains useful methods for common array operations such as sorting and searching. For example, the following code sorts an array of numbers and an array of characters. double[ ] numbers = {6.0,4.4,1.9,2.9,3.4,3.5 }; java.util.arrays.sort(numbers); // Sort the whole array char[ ] chars = { a {, A, 4, F, D, P }; java.util.arrays.sort(chars,1, 3);// Sort part of the array from chars[1] to chars[3-1] Prentice Hall, Inc. All rights reserved.

41 The Arrays Class, cont. Outline For example, the following code searches the keys in an array of integers and an array of characters. int[ [] list = {2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79}; System.out.println( (1) (1) Index is +java.util util.arrays.binarysearch(list,11 )); System.out.println( (2) (2) Index is +java.util util.arrays.binarysearch(list,12 )); char [ ] chars = { a,{ c, g, x, y, z }; System.out.println( (3) (3) Index is +java.util util.arrays.binarysearch(chars, chars, a )); System.out.println( (3) (3) Index is +java.util util.arrays.binarysearch(chars, chars, t )); The output of the preceding code is 1. Index is 4 2. Index is 6 3. Index is 0 4. Index is Prentice Hall, Inc. All rights reserved.

Chapter 6 Single-Dimensional Arrays. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 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. Chapter 6 Single-Dimensional Arrays rights reserved. 1 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average. Solution AnalyzeNumbers rights

More information

14. Array Basics. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

14. Array Basics. Java. Summer 2008 Instructor: Dr. Masoud Yaghini 14. Array Basics Java Summer 2008 Instructor: Dr. Masoud Yaghini Outline Introduction Declaring an Array Creating Arrays Accessing an Array Simple Processing on Arrays Copying Arrays References Introduction

More information

Arrays (Deitel chapter 7)

Arrays (Deitel chapter 7) Arrays (Deitel chapter 7) Plan Arrays Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods Sorting Arrays Searching Arrays: Linear Search and

More information

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

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved 1 To describe why arrays are necessary in programming ( 6.1). To declare array reference variables and create arrays ( 6.2.1-6.2.2). To initialize the values in an array ( 6.2.3). To access array elements

More information

Chapter 7 Single-Dimensional Arrays

Chapter 7 Single-Dimensional Arrays Chapter 7 Single-Dimensional Arrays 1 Arrays Array is a data structure that represents a collection of same-types data elements. A single-dimensional array is one that stores data elements in one row.

More information

array Indexed same type

array Indexed same type ARRAYS Spring 2019 ARRAY BASICS An array is an indexed collection of data elements of the same type Indexed means that the elements are numbered (starting at 0) The restriction of the same type is important,

More information

CS115 Principles of Computer Science

CS115 Principles of Computer Science CS5 Principles of Computer Science Chapter Arrays Prof. Joe X. Zhou Department of Computer Science CS5 Arrays. Re: Objectives in Methods To declare methods, invoke methods, and pass arguments to a method

More information

Opening Problem EXAMPLE. 1. Read one hundred numbers, 2. compute their average, and 3. find out how many numbers are above the average.

Opening Problem EXAMPLE. 1. Read one hundred numbers, 2. compute their average, and 3. find out how many numbers are above the average. Chapter 6 Arrays 1 Opening Problem EXAMPLE 1. Read one hundred numbers, 2. compute their average, and 3. find out how many numbers are above the average. 2 Introducing Arrays Array is a data structure

More information

Chapter 7: Single-Dimensional Arrays. Declaring Array Variables. Creating Arrays. Declaring and Creating in One Step.

Chapter 7: Single-Dimensional Arrays. Declaring Array Variables. Creating Arrays. Declaring and Creating in One Step. Chapter 7: Single-Dimensional Arrays Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average CS1: Java Programming Colorado State University

More information

Module 7: Arrays (Single Dimensional)

Module 7: Arrays (Single Dimensional) Module 7: Arrays (Single Dimensional) Objectives To describe why arrays are necessary in programming ( 7.1). To declare array reference variables and create arrays ( 7.2.1 7.2.2). To obtain array size

More information

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

Arrays. Introduction to OOP with Java. Lecture 06: Introduction to OOP with Java - AKF Sep AbuKhleiF -  1 Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017 Lecture 06: Arrays Instructor: AbuKhleif, Mohammad Noor Sep 2017 AbuKhleiF - 1 Instructor AbuKhleif, Mohammad Noor Computer Engineer

More information

Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world

Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world Visit us on the World Wide Web at: www.pearsoned.co.uk Pearson Education Limited 2014

More information

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types COMP-202 Unit 6: Arrays Introduction (1) Suppose you want to write a program that asks the user to enter the numeric final grades of 350 COMP-202

More information

Programming for Engineers Arrays

Programming for Engineers Arrays Programming for Engineers Arrays ICEN 200 Spring 2018 Prof. Dola Saha 1 Array Ø Arrays are data structures consisting of related data items of the same type. Ø A group of contiguous memory locations that

More information

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

Announcements. PS 4 is ready, due next Thursday, 9:00pm. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am Announcements PS 4 is ready, due next Thursday, 9:00pm Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am Room TBD Scope: Lecture 1 to Lecture 9 (Chapters 1 to 6 of text) You may bring a sheet of paper (A4, both

More information

Arrays. Eng. Mohammed Abdualal

Arrays. Eng. Mohammed Abdualal 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

More information

Chapter 6 Arrays and Strings Prentice Hall, Inc. All rights reserved.

Chapter 6 Arrays and Strings Prentice Hall, Inc. All rights reserved. 1 Chapter 6 Arrays and Strings Introduction 2 Arrays Data structures Related data items of same type Reference type Remain same size once created Fixed-length entries 3 Name of array (note that all elements

More information

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

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters Outline Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters Variable Length Parameter Lists split() Method from String Class Integer & Double Wrapper

More information

15. Arrays and Methods. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

15. Arrays and Methods. Java. Summer 2008 Instructor: Dr. Masoud Yaghini 15. Java Summer 2008 Instructor: Dr. Masoud Yaghini Outline Passing Arrays to Methods Returning an Array from a Method Variable-Length Argument Lists References Passing Arrays to Methods Passing Arrays

More information

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7) Array Basics: Outline Arrays (Savitch, Chapter 7) TOPICS Array Basics Arrays in Classes and Methods Programming with Arrays Searching and Sorting Arrays Multi-Dimensional Arrays Static Variables and Constants

More information

Arrays Introduction. Group of contiguous memory locations. Each memory location has same name Each memory location has same type

Arrays Introduction. Group of contiguous memory locations. Each memory location has same name Each memory location has same type Array Arrays Introduction Group of contiguous memory locations Each memory location has same name Each memory location has same type Remain same size once created Static entries 1 Name of array (Note that

More information

Outline. 7.1 Introduction. 7.2 Arrays. 7.2 Arrays

Outline. 7.1 Introduction. 7.2 Arrays. 7.2 Arrays jhtp5_07.fm Page 279 Wednesday, November 20, 2002 12:44 PM 7 Arrays Objectives To introduce the array data structure. To understand the use of arrays to store, sort and search lists and tables of values.

More information

CS1150 Principles of Computer Science Arrays

CS1150 Principles of Computer Science Arrays CS1150 Principles of Computer Science Arrays Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Colorado Springs Opening Problem Read one hundred numbers, compute their

More information

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved. C How to Program, 7/e This chapter serves as an introduction to data structures. Arrays are data structures consisting of related data items of the same type. In Chapter 10, we discuss C s notion of

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

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

Array. Lecture 12. Based on Slides of Dr. Norazah Yusof Array Lecture 12 Based on Slides of Dr. Norazah Yusof 1 Introducing Arrays Array is a data structure that represents a collection of the same types of data. In Java, array is an object that can store a

More information

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

Last Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it Last Class Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it public class February4{ public static void main(string[] args) { String[]

More information

Object Oriented Programming. Java-Lecture 6 - Arrays

Object Oriented Programming. Java-Lecture 6 - Arrays Object Oriented Programming Java-Lecture 6 - Arrays Arrays Arrays are data structures consisting of related data items of the same type In Java arrays are objects -> they are considered reference types

More information

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd El-Shorouk Academy Acad. Year : 2013 / 2014 High Institute of Computer Science & Information Technology Term : 1 st Year : 2 nd Computer Science Department Object Oriented Programming Section (1) Arrays

More information

Chapter 9 Introduction to Arrays. Fundamentals of Java

Chapter 9 Introduction to Arrays. Fundamentals of Java Chapter 9 Introduction to Arrays Objectives Write programs that handle collections of similar items. Declare array variables and instantiate array objects. Manipulate arrays with loops, including the enhanced

More information

Example: Computing prime numbers

Example: Computing prime numbers Example: Computing prime numbers -Write a program that lists all of the prime numbers from 1 to 10,000. Remember a prime number is a # that is divisible only by 1 and itself Suggestion: It probably will

More information

Multiple-Subscripted Arrays

Multiple-Subscripted Arrays Arrays in C can have multiple subscripts. A common use of multiple-subscripted arrays (also called multidimensional arrays) is to represent tables of values consisting of information arranged in rows and

More information

Lecture #8-10 Arrays

Lecture #8-10 Arrays Lecture #8-10 Arrays 1. Array data structure designed to store a fixed-size sequential collection of elements of the same type collection of variables of the same type 2. Array Declarations Creates a Storage

More information

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;

More information

Instructor: Eng.Omar Al-Nahal

Instructor: Eng.Omar Al-Nahal Faculty of Engineering & Information Technology Software Engineering Department Computer Science [2] Lab 6: Introduction in arrays Declaring and Creating Arrays Multidimensional Arrays Instructor: Eng.Omar

More information

C++ PROGRAMMING SKILLS Part 4: Arrays

C++ PROGRAMMING SKILLS Part 4: Arrays C++ PROGRAMMING SKILLS Part 4: Arrays Outline Introduction to Arrays Declaring and Initializing Arrays Examples Using Arrays Sorting Arrays: Bubble Sort Passing Arrays to Functions Computing Mean, Median

More information

Lecture 17. Instructor: Craig Duckett. Passing & Returning Arrays

Lecture 17. Instructor: Craig Duckett. Passing & Returning Arrays Lecture 17 Instructor: Craig Duckett Passing & Returning Arrays Assignment Dates (By Due Date) Assignment 1 (LECTURE 5) GRADED! Section 1: Monday, January 22 nd Assignment 2 (LECTURE 8) GRADED! Section

More information

Array. Prepared By - Rifat Shahriyar

Array. Prepared By - Rifat Shahriyar Java More Details Array 2 Arrays A group of variables containing values that all have the same type Arrays are fixed length entities In Java, arrays are objects, so they are considered reference types

More information

Chapter 5 - Methods Prentice Hall, Inc. All rights reserved.

Chapter 5 - Methods Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Methods 2003 Prentice Hall, Inc. All rights reserved. 2 Introduction Modules Small pieces of a problem e.g., divide and conquer Facilitate design, implementation, operation and maintenance

More information

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class? Administration Objects and Arrays CS 99 Summer 2000 Michael Clarkson Lecture 6 Read clarified grading policies Lab 6 due tomorrow Submit.java files in a folder named Lab6 Lab 7 Posted today Upson Lab closed

More information

Arrays. Lecture 11 CGS 3416 Spring March 6, Lecture 11CGS 3416 Spring 2017 Arrays March 6, / 19

Arrays. Lecture 11 CGS 3416 Spring March 6, Lecture 11CGS 3416 Spring 2017 Arrays March 6, / 19 Arrays Lecture 11 CGS 3416 Spring 2017 March 6, 2017 Lecture 11CGS 3416 Spring 2017 Arrays March 6, 2017 1 / 19 Arrays Definition: An array is an indexed collection of data elements of the same type. Indexed

More information

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

Last Class. More on loops break continue A bit on arrays Last Class More on loops break continue A bit on arrays public class February2{ public static void main(string[] args) { String[] allsubjects = { ReviewArray, Example + arrays, obo errors, 2darrays };

More information

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

Lab Session # 5 Arrays. ALQUDS University Department of Computer Engineering 2013/2014 Programming Fundamentals for Engineers Lab Lab Session # 5 Arrays ALQUDS University Department of Computer Engineering Objective: After completing this session, the students should be able to:

More information

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

More information

Chapter 7 Array. Array. C++, How to Program

Chapter 7 Array. Array. C++, How to Program Chapter 7 Array C++, How to Program Deitel & Deitel Spring 2016 CISC 1600 Yanjun Li 1 Array Arrays are data structures containing related data items of same type. An array is a consecutive group of memory

More information

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015 Arrays Lecture 11 CGS 3416 Fall 2015 October 26, 2015 Arrays Definition: An array is an indexed collection of data elements of the same type. Indexed means that the array elements are numbered (starting

More information

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Object oriented programming. Instructor: Masoud Asghari Web page:   Ch: 3 Object oriented programming Instructor: Masoud Asghari Web page: http://www.masses.ir/lectures/oops2017sut Ch: 3 1 In this slide We follow: https://docs.oracle.com/javase/tutorial/index.html Trail: Learning

More information

BTE2313. Chapter 2: Introduction to C++ Programming

BTE2313. Chapter 2: Introduction to C++ Programming For updated version, please click on http://ocw.ump.edu.my BTE2313 Chapter 2: Introduction to C++ Programming by Sulastri Abdul Manap Faculty of Engineering Technology sulastri@ump.edu.my Objectives In

More information

1 Lexical Considerations

1 Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler

More information

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays A First Book of ANSI C Fourth Edition Chapter 8 Arrays Objectives One-Dimensional Arrays Array Initialization Arrays as Function Arguments Case Study: Computing Averages and Standard Deviations Two-Dimensional

More information

Computational Expression

Computational Expression Computational Expression Variables, Primitive Data Types, Expressions Janyl Jumadinova 28-30 January, 2019 Janyl Jumadinova Computational Expression 28-30 January, 2019 1 / 17 Variables Variable is a name

More information

Introduction to Java & Fundamental Data Types

Introduction to Java & Fundamental Data Types Introduction to Java & Fundamental Data Types LECTURER: ATHENA TOUMBOURI How to Create a New Java Project in Eclipse Eclipse is one of the most popular development environments for Java, as it contains

More information

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. C How to Program, 6/e 1992-2010 by Pearson Education, Inc. 1992-2010 by Pearson Education, Inc. 1992-2010 by Pearson Education, Inc. This chapter serves as an introduction to the important topic of data

More information

Review of Important Topics in CS1600. Functions Arrays C-strings

Review of Important Topics in CS1600. Functions Arrays C-strings Review of Important Topics in CS1600 Functions Arrays C-strings Array Basics Arrays An array is used to process a collection of data of the same type Examples: A list of names A list of temperatures Why

More information

Programming with Java

Programming with Java Programming with Java Data Types & Input Statement Lecture 04 First stage Software Engineering Dep. Saman M. Omer 2017-2018 Objectives q By the end of this lecture you should be able to : ü Know rules

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 10 For Loops and Arrays Outline Problem: How can I perform the same operations a fixed number of times? Considering for loops Performs same operations

More information

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers

More information

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

Arrays Data structures Related data items of same type Remain same size once created Fixed-length entries CBOP3203 Arrays Data structures Related data items of same type Remain same size once created Fixed-length entries A 12 element Array Index Also called subscript Position number in square brackets Must

More information

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

Java-Array. This tutorial introduces how to declare array variables, create arrays, and process arrays using indexed variables. -Array Java provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful

More information

EC312 Chapter 4: Arrays and Strings

EC312 Chapter 4: Arrays and Strings Objectives: (a) Describe how an array is stored in memory. (b) Define a string, and describe how strings are stored. EC312 Chapter 4: Arrays and Strings (c) Describe the implications of reading or writing

More information

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani Data Types, Variables and Arrays OOC 4 th Sem, B Div 2016-17 Prof. Mouna M. Naravani Identifiers in Java Identifiers are the names of variables, methods, classes, packages and interfaces. Identifiers must

More information

Arrays. Outline 1/7/2011. Arrays. Arrays are objects that help us organize large amounts of information. Chapter 7 focuses on:

Arrays. Outline 1/7/2011. Arrays. Arrays are objects that help us organize large amounts of information. Chapter 7 focuses on: Arrays Arrays Arrays are objects that help us organize large amounts of information Chapter 7 focuses on: array declaration and use bounds checking and capacity arrays that store object references variable

More information

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette COMP 250: Java Programming I Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette Variables and types [Downey Ch 2] Variable: temporary storage location in memory.

More information

TOPICS TO COVER:-- Array declaration and use.

TOPICS TO COVER:-- Array declaration and use. ARRAYS in JAVA TOPICS TO COVER:-- Array declaration and use. One-Dimensional Arrays. Passing arrays and array elements as parameters Arrays of objects Searching an array Sorting elements in an array ARRAYS

More information

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science Department Lecture 3: C# language basics Lecture Contents 2 C# basics Conditions Loops Methods Arrays Dr. Amal Khalifa, Spr 2015 3 Conditions and

More information

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long Primitive Types Four integer types: byte short int (most common) long Two floating-point types: float double (most common) One character type: char One boolean type: boolean 1 2 Primitive Types, cont.

More information

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I BASIC COMPUTATION x public static void main(string [] args) Fundamentals of Computer Science I Outline Using Eclipse Data Types Variables Primitive and Class Data Types Expressions Declaration Assignment

More information

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2010 Handout Decaf Language Tuesday, Feb 2 The project for the course is to write a compiler

More information

Last Class. While loops Infinite loops Loop counters Iterations

Last Class. While loops Infinite loops Loop counters Iterations Last Class While loops Infinite loops Loop counters Iterations public class January31{ public static void main(string[] args) { while (true) { forloops(); if (checkclassunderstands() ) { break; } teacharrays();

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II 1 CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1: Introduction Lecture Contents 2 Course info Why programming?? Why Java?? Write once, run anywhere!! Java basics Input/output Variables

More information

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010 IPCoreL Programming Language Reference Manual Phillip Duane Douglas, Jr. 11/3/2010 The IPCoreL Programming Language Reference Manual provides concise information about the grammar, syntax, semantics, and

More information

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 6 Decaf Language Wednesday, September 7 The project for the course is to write a

More information

EXERCISES SOFTWARE DEVELOPMENT I. 04 Arrays & Methods 2018W

EXERCISES SOFTWARE DEVELOPMENT I. 04 Arrays & Methods 2018W EXERCISES SOFTWARE DEVELOPMENT I 04 Arrays & Methods 2018W Solution First Test DATA TYPES, BRANCHING AND LOOPS Complete the following program, which calculates the price for a car rental. First, the program

More information

Lecture 14. 'for' loops and Arrays

Lecture 14. 'for' loops and Arrays Lecture 14 'for' loops and Arrays For Loops for (initiating statement; conditional statement; next statement) // usually incremental body statement(s); The for statement provides a compact way to iterate

More information

2. [20] Suppose we start declaring a Rectangle class as follows:

2. [20] Suppose we start declaring a Rectangle class as follows: 1. [8] Create declarations for each of the following. You do not need to provide any constructors or method definitions. (a) The instance variables of a class to hold information on a Minesweeper cell:

More information

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

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods Islamic University of Gaza Faculty of Engineering Computer Engineering Dept. Computer Programming Lab (ECOM 2114) ABSTRACT In this Lab you will learn to define and invoke void and return java methods JAVA

More information

Course PJL. Arithmetic Operations

Course PJL. Arithmetic Operations Outline Oman College of Management and Technology Course 503200 PJL Handout 5 Arithmetic Operations CS/MIS Department 1 // Fig. 2.9: Addition.java 2 // Addition program that displays the sum of two numbers.

More information

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Dr. Marenglen Biba Before writing a program to solve a problem, have a thorough understanding of the problem and a carefully planned approach to solving it. Understand the types of building

More information

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018 Pointer Basics Lecture 13 COP 3014 Spring 2018 March 28, 2018 What is a Pointer? A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory

More information

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1 topics: introduction to java, part 1 cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 cis20.1-fall2007-sklar-leci.2 1 Java. Java is an object-oriented language: it is

More information

C++ Arrays and Vectors

C++ Arrays and Vectors C++ Arrays and Vectors Contents 1 Overview of Arrays and Vectors 2 2 Arrays 3 2.1 Declaring Arrays................................................. 3 2.2 Initializing Arrays................................................

More information

A Quick and Dirty Overview of Java and. Java Programming

A Quick and Dirty Overview of Java and. Java Programming Department of Computer Science New Mexico State University. CS 272 A Quick and Dirty Overview of Java and.......... Java Programming . Introduction Objectives In this document we will provide a very brief

More information

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI CSCI 2010 Principles of Computer Science Data and Expressions 08/09/2013 CSCI 2010 1 Data Types, Variables and Expressions in Java We look at the primitive data types, strings and expressions that are

More information

Lecture 15. Arrays (and For Loops)

Lecture 15. Arrays (and For Loops) Lecture 15 Arrays (and For Loops) For Loops for (initiating statement; conditional statement; next statement) // usually incremental { body statement(s); The for statement provides a compact way to iterate

More information

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections C++ Programming Chapter 6 Arrays and Vectors Yih-Peng Chiou Room 617, BL Building (02) 3366-3603 3603 ypchiou@cc.ee.ntu.edu.tw Photonic Modeling and Design Lab. Graduate Institute of Photonics and Optoelectronics

More information

Chapter 7 : Arrays (pp )

Chapter 7 : Arrays (pp ) Page 1 of 45 Printer Friendly Version User Name: Stephen Castleberry email Id: scastleberry@rivercityscience.org Book: A First Book of C++ 2007 Cengage Learning Inc. All rights reserved. No part of this

More information

Lesson 9: Introduction To Arrays (Updated for Java 1.5 Modifications by Mr. Dave Clausen)

Lesson 9: Introduction To Arrays (Updated for Java 1.5 Modifications by Mr. Dave Clausen) Lesson 9: Introduction To Arrays (Updated for Java 1.5 Modifications by Mr. Dave Clausen) 1 Lesson 9: Introduction Objectives: To Arrays Write programs that handle collections of similar items. Declare

More information

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M Università degli Studi di Bologna Facoltà di Ingegneria Principles, Models, and Applications for Distributed Systems M tutor Isam M. Al Jawarneh, PhD student isam.aljawarneh3@unibo.it Mobile Middleware

More information

Software Practice 1 Basic Grammar

Software Practice 1 Basic Grammar Software Practice 1 Basic Grammar Basic Syntax Data Type Loop Control Making Decision Prof. Joonwon Lee T.A. Jaehyun Song Jongseok Kim (42) T.A. Sujin Oh Junseong Lee (43) 1 2 Java Program //package details

More information

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays) Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays) In this lecture, you will: Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of

More information

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 1 ARRAYS Arrays 2 Arrays Structures of related data items Static entity (same size

More information

The first applet we shall build will ask the user how many times the die is to be tossed. The request is made by utilizing a JoptionPane input form:

The first applet we shall build will ask the user how many times the die is to be tossed. The request is made by utilizing a JoptionPane input form: Lecture 5 In this lecture we shall discuss the technique of constructing user-defined methods in a class. The discussion will be centered about an experiment of tossing a die a specified number of times.

More information

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) Chapter - Arrays 1.1 Introduction 2.1 Introduction.2 Arrays.3 Declaring Arrays. Examples Using Arrays.5 Passing Arrays to Functions.6 Sorting Arrays. Case Study: Computing Mean, Median and Mode Using Arrays.8

More information

Objectives. In this chapter, you will:

Objectives. In this chapter, you will: Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates arithmetic expressions Learn about

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Java Primitive Data Types; Arithmetic Expressions Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu

More information

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example Admin CS 112 Introduction to Programming q Programming assignment 2 to be posted tonight Java Primitive Data Types; Arithmetic Expressions Yang (Richard) Yang Computer Science Department Yale University

More information

Chapter 2 Basic Elements of C++

Chapter 2 Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 2-1 Chapter 2 Basic Elements of C++ At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion

More information

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand what an array is Understand how to declare arrays Understand what reference variables are Understand how to pass arrays to methods

More information