Single-Dimensional Arrays chapter6

Slides:



Advertisements
Similar presentations
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Advertisements

Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Chapter 7 Arrays.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Array Must declare a variable to reference the array double [] mylist; // cannot double list[20]; Or double mylist[]; The declaration doesn’t allocate.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
1 Chapter 7 Single-Dimensional Arrays. 2 Arrays Array is a data structure that represents a collection of the same types of data elements. A single-dimensional.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
 Review  Quick reference Announcement. Chapter 1: Basics.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Liang, Introduction to Java Programming1 Arrays Gang Qian Department of Computer Science University of Central Oklahoma.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Lab 8. Declaring and Creating Arrays in One Step datatype[] arrayRefVar = new datatype[arraySize]; double[] myList = new double[10];
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Arrays.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
Chapter 6 Arrays 1 Fall 2012 CS2302: Programming Principles.
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 6 Arrays 1.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 7 Single-Dimensional Arrays.
IAS 1313: OBJECT ORIENTED PROGRAMMING Week 3: Data Type, Control Structure and Array Prepared by: Mrs Sivabalan1.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Lecture 7: Arrays Michael Hsu CSULA 3 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average.
Single Dimensional Arrays
Chapter 7: Single-Dimensional Arrays
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. System.out.println(“Please enter grade.
Chapter 7 Single-Dimensional Arrays
Chapter 6 Arrays.
Chapter 7 Single-Dimensional Arrays
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. cout >
Chapter 7 Single-Dimensional Arrays
Chapter 6 Arrays.
Chapter 6 Arrays.
Chapter 6 Arrays.
Chapter 7 Arrays Data structures Related data items of same type
Chapter 6 Arrays.
Chapter 6 Arrays Solution Opening Problem
Introducing Arrays Array is a data structure that represents a collection of the same types of data.
METHODS (FUNCTIONS) By: Lohani Adeeb khan.
Chapter 6 Arrays Fall 2012 CS2302: Programming Principles.
Chapter 5 Arrays Introducing Arrays
Object Oriented Programming in java
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
CS2011 Introduction to Programming I Arrays (I)
Lab5 PROGRAMMING 1 Loop chapter4.
elementary programming
SELECTIONS STATEMENTS
Chapter 6 Arrays.
Single-Dimensional Arrays
Arrays in Java.
Chapter 6 Arrays.
Chapter 6 Arrays.
Chapter 5 Arrays.
Lab6 PROGRAMMING 1 metheds chapter5.
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Lab4 PROGRAMMING 1 exercise chapter3.
Presentation transcript:

Single-Dimensional Arrays chapter6 lab8 PROGRAMMING 1 Single-Dimensional Arrays chapter6

BEFORE WE START Your Cell Phone Silent Please Keep Quite Find Your Computer and Switch On Ins.Ebtesam AL-Etowi

content Array Basics. Array Type . To declare array reference variables and create arrays. To initialize the values in an array. To access array elements using indexed variables. To declare, create, and initialize an array using an array initializer. To copy contents from one array to another. To search elements using the linear. Enhanced for Loop (for-each loop). To use the methods in the Arrays class. Ins.Ebtesam AL-etowi

Array Basics An array is used to store a collection of data, but often we find it more useful to think of an array as a collection of variables of the same type. Java and most other high-level languages provide a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. Instead of declaring individual variables, such as number0, number1, and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and numbers[99] to represent individual variables. Ins.Ebtesam AL-etowi

Array Type Ins.Ebtesam AL-etowi

Declaring Array Variables To use an array in a program, you must declare a variable to reference the array and specify the array’s element type. Here is the syntax for declaring an array variable: elementType [ ] arrayRefVar; OR elementType arrayRefVar[ ] ; The elementType can be any data type, and all elements in the array will have the same datatype. For example, the following code declares a variable myList that references an array of double elements. double[ ] myList; Ins.Ebtesam AL-etowi

Creating Arrays Unlike declarations for primitive data type variables, the declaration of an array variable does not allocate any space in memory for the array. It creates only a storage location for the reference to an array. If a variable does not contain a reference to an array, the value of the variable is null. After an array variable is declared, you can create an array by using the new operator with the following syntax: arrayRefVar = new elementType[arraySize]; This statement does two things: (1) it creates an array using new elementType[array-Size]; (2) it assigns the reference of the newly created array to the variable arrayRefVar. Example : double[ ] myList = new double[10]; double[ ] myList; OR myList = new double[10]; double myList [ ] = new double[10]; Ins.Ebtesam AL-etowi

Example The size of an array cannot be changed after the array is created. Size can be obtained using arrayRefVar.length. For example: myList.length is 10. When an array is created, its elements are assigned the default value of 0 for the numeric. myList holds ten double values, and the indices are from 0 to 9. Ins.Ebtesam AL-etowi

Array Size and Default Values The size of an array cannot be changed after the array is created. Size can be obtained using arrayRefVar.length. For example, myList.length is 10. When an array is created, its elements are assigned the default value of 0 for the numericprimitive data types, '\u0000' for char types, and false for boolean types. Array Indexed Variables The array elements are accessed through the index. Array indices are 0 based; that is, they range from 0 to arrayRefVar.length-1. myList holds ten double values, and the indices are from 0 to 9. Each element in the array is represented using the following syntax, known as an indexed variable: arrayRefVar[index]; For example, myList[9] represents the last element in the array myList. Ins.Ebtesam AL-etowi

Splitting it would cause a syntax error Array Initializers Java has a shorthand notation, known as the array initializer, which combines in one statement declaring an array, creating an array, and initializing, using the following syntax elementType[ ] arrayRefVar = {value0, value1, ..., valuek}; double[ ] myList = {1.9, 2.9, 3.4, 3.5}; CAUTION which is equivalent to the statements shown below: all in one statement double[ ] myList = new double[4]; myList[0] = 1.9; myList[1] = 2.9; myList[2] = 3.4; myList[3] = 3.5; Splitting it would cause a syntax error Ins.Ebtesam AL-etowi

Copying Arrays There are different ways to copy arrays: 1-Use a loop to copy individual elements one by one:. list2 = list1; Often, in a program, you need to duplicate an array or a part of an array. In such cases you could attempt to use the assignment statement (=), as follows: for (int i = 0; i < sourceArray.length; i++) { targetArray[i] = sourceArray[i]; } 2-Use the static arraycopy method in the System class: use the arraycopy method in the java.lang.System class to copyarrays instead of using a loop. The syntax for arraycopy is shown below: arraycopy(sourceArray, src_pos, targetArray, tar_pos, length); System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length); int arry1[ ]={1,2,3}; int arry2[ ]={0,0,0}; System.arraycopy(arry1, 0, arry2, 0, 3); Ins.Ebtesam AL-etowi

Passing Arrays to Methods public class Examplearry { public static void main(String[] args) { int[ ] y = {3, 1, 2, 6, 4, 2}; printArray(y); } public static void printArray(int[ ] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); Arry name pass 31 2 6 4 2 output Ins.Ebtesam AL-etowi

Returning an Array from a Method 1 2 List={1,2,3} result={3,2,1} Ins.Ebtesam AL-etowi

Searching Arrays Key is value the user enter to search in arry ,if found the value return the index of value . Ins.Ebtesam AL-etowi

Enhanced for Loop (for-each loop) introduced a new for loop that enables you to traverse the complete array sequentially without using an index variable. For example, the following code displays all elements in the array myList: for (double value: myList) System.out.println(value); Example Output : Ins.Ebtesam AL-etowi

The Arrays Class The java.util.Arrays class contains various static methods for sorting and searching arrays, comparing arrays, and filling array elements double[ ] numbers = {6.0, 4.4, 1.9, 2.9, 3.4, 3.5}; java.util.Arrays.sort(numbers); java.util.Arrays.binarySearch(number,3.4); java.util.Arrays.fill(number, 5.3); System.out.println (Arrays.toString(number)); Ins.Ebtesam AL-etowi

Explain The Exercise Initialize double an array with the marks of 5 subjects and calculate the average.??? Ins.Ebtesam AL-etowi

Exercise Ins.Ebtesam AL-etowi

Cont….. Output: Ins.Ebtesam AL-etowi

Thank You !