1. Define an array 1 Create reference arrays of objects in Java program 2 Initialize elements of arrays 3 Pass array to methods 4 Return array to methods.

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Arrays.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Arrays part 3 Multidimensional arrays, odds & ends.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
Multidimensional Arrays in Java Vidhu S. Kapadia.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Arrays. Example Write a program to keep track of all students’ scores on exam 1. Need a list of everyone’s score Declare 14 double variables? What about.
Multiple-Subscripted Array
Building Java Programs Chapter 7.5
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
Java Unit 9: Arrays Declaring and Processing Arrays.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
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.
Program structure Four different storage-class specifications: –Automatic (auto): local to a function, normally declared variables are automatic. Does.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays. Arrays in Java  Arrays in Java are objects.  Like all objects are created with the new keyword.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Pemrograman Dasar Arrays PTIIK - UB. Arrays  An array is a container object that holds a fixed number of values of a single type.  The length of an.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
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.
2D-Arrays Quratulain. Learning Objectives Two-dimensional arrays Declaration Initialization Applications.
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.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
© 2004 Pearson Addison-Wesley. All rights reserved October 13, D Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor:
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
FP201 - PROGRAMMING FUNDAMENTALS Unit Understand the use of array PREPARED BY: MAZNAH AHMAD, JTMK PSIS.
Arrays.
Arrays.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
CiS 260: App Dev I. 2 Introduction to Arrays n An array is an object that contains a collection of components (_________) of the same data type. n For.
CiS 260: App Dev I. 2 Introduction to Arrays n An array is an object that contains a collection of components (_________) of the same data type. n For.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
In C programming, one of the frequently arising problem is to handle similar types of data. For example: If the user want to store marks of 100 students.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
Arrays Chapter 12. One-Dimensional Arrays If you wanted to read in 1000 ints and print them in reverse order, it would take a program that’s over 3000.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
Multidimensional Arrays
CHAPTER 2 Arrays and Vectors.
CHAPTER 2 Arrays and Vectors.
C++ Array 1.
Presentation transcript:

1

Define an array 1 Create reference arrays of objects in Java program 2 Initialize elements of arrays 3 Pass array to methods 4 Return array to methods 5 Write program using single and multidimensional array 6 2

3 Collection of data values of the same type. Each element in the array is referred by its subscript (position number/index). in Java, array is an object – need to use the new method during declaration.

To declare an array, write the data type, followed by a set of square brackets[], followed by the identifier name. Example 1: Declaring array int[] ages; or int ages[]; Example 2: Creating array object/instance float[] studsMark = new float[5]; //create object/instance and refer the object as studMarks. //studMarks can be used to store up to 5 float values 4

5

6 String[] names; names = new String[3]; names[0] = "Georgianna"; names[1] = "Jen"; names[2] = "Simon"; String[] names; names = new String[3]; names[0] = "Georgianna"; names[1] = "Jen"; names[2] = "Simon"; MyDate[] dates; dates = new MyDate[3]; dates[0] = new MyDate(22, 7, 1964); dates[1] = new MyDate(1, 1, 2000); dates[2] = new MyDate(22, 12, 1964); MyDate[] dates; dates = new MyDate[3]; dates[0] = new MyDate(22, 7, 1964); dates[1] = new MyDate(1, 1, 2000); dates[2] = new MyDate(22, 12, 1964); String[] names = { "Georgianna", "Jen", "Simon" }; String[] names = { "Georgianna", "Jen", "Simon" }; MyDate[] dates = { new MyDate(22, 7, 1964), new MyDate(1, 1, 2000), new MyDate(22, 12, 1964) }; MyDate[] dates = { new MyDate(22, 7, 1964), new MyDate(1, 1, 2000), new MyDate(22, 12, 1964) };

7 When a single element of an array is passed to a method it is handled like any other variable. public class PassElements { public static void main(String[]arg) { int [] num={5,10,15,20,25,30,35,40}; for(int i=0;i<num.length;i++) showValue(num[i]); } public static void showValue(int n) { System.out.println(n+ " "); } public class PassElements { public static void main(String[]arg) { int [] num={5,10,15,20,25,30,35,40}; for(int i=0;i<num.length;i++) showValue(num[i]); } public static void showValue(int n) { System.out.println(n+ " "); }

8 public class PassElements { public static void main(String[]arg) { int [] num={5,10,15,20,25,30,35,40}; PassElements pass = new PassElements(); for(int i=0;i<num.length;i++) pass.showValue(num[i]); } public void showValue(int n) { System.out.println(n+ " "); } public class PassElements { public static void main(String[]arg) { int [] num={5,10,15,20,25,30,35,40}; PassElements pass = new PassElements(); for(int i=0;i<num.length;i++) pass.showValue(num[i]); } public void showValue(int n) { System.out.println(n+ " "); }

9 A method can return a reference to an array. The return type of the method must be declared as an array of the right type. public static double[] getArray() { double[] array = { 1.2, 2.3, 4.5, 6.7, 8.9 }; return array; } The getArray method is a public static method that returns an array of doubles.

10 Are arrays with more than one dimension. Is a collection of a number of one-dimensional arrays placed one below the other. Two-dimensional arrays represent data in terms of rows and columns. It has two subscripts. Example: int[][] twoDim = new int[4][5]; Array of four arrays of five integers each. Multidimensional Array:

11

Assume that there are 5 students in a class and each of them study three different subjects, for example Mathematics, Physics and Chemistry. The marks of each student in all three subjects can be represented in a single row. 12

13

14

Syntax [][] = new int[Row][Column]; Example int marks_table[][] = new int[5][3]; 15