Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.

Slides:



Advertisements
Similar presentations
Chapter 21 Implementing lists: array implementation.
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Programming and Data Structure
Structure.
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.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Introduction to arrays Data in economy size packages.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Java Syntax Primitive data types Operators Control statements.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
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.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
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.
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
1 Arrays An array is a collection of data values, all of which have the same type. The size of the array is fixed at creation. To refer to specific values.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Arrays. 2 Till now we are able to declare and initialize few variables Reality: need to compute on a large amount of data Arrays are data structures that.
CIS 270—Application Development II Chapter 18-Generics.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Simple Arrays Arrays of primitives and Strings Sections 7.1, 7.2.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Variables and memory addresses
Chapter 10 Arrays. Learning Java through Alice © Daly and Wrigley Objectives Declare and use arrays in programs. Access array elements within an array.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
CS1020 Data Structures and Algorithms I Lecture Note #2 Arrays.
1 Arrays and Methods Java always passes arguments by value – that is a copy of the value is made in the called method and this is modified in the method.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Introduction to Computing Concepts Note Set 21. Arrays Declaring Initializing Accessing Using with Loops Sending to methods.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
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 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
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.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
Array and String.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Introduction to programming in java Lecture 21 Arrays – Part 1.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Chapter VII: Arrays.
Objects as a programming concept
Introduction to Computing Using Java
Java Arrays. Array Object An array :a container object holds a fixed number of values of a single type. The length of an array is established when the.
An Introduction to Java – Part I, language basics
Defining methods and more arrays
CS2011 Introduction to Programming I Methods (II)
CSC 142 Arrays [Reading: chapter 12].
Arrays in Java.
Names of variables, functions, classes
Introduction to java Part I By Shenglan Zhang.
Week 7 - Monday CS 121.
Presentation transcript:

Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What if we want to store lots of items – e.g. midsem marks?

Arrays Arrays are data structures that can hold a series of values –An array uses an index to access a particular value

Primitive Type Syntax double number = 3;

Array Syntax double[ ] number;

Array Syntax double[ ] number = {1,2,3} ; String s[ ] = {"This", "is", "an", "array"};

Array Syntax double[ ] number; Can also initialize with the “ new ” construct: double [ ] number = new double[3];

Array Implementation Every time an array is initialized, using new, or by {… }, a contiguous block of memory is assigned for it the array name (the identifier number) is assigned the start address (In Java, this address is the reference for this variable). number[3] gets an address 3 shifted from the start

Parallel Arrays int[ ] rollNum= {6016, 6024, 6078}; double[ ] midsemMarks = {61.7, 54, 74.2 }; String[ ] name = {"AbhishekA", "AbhishekS", "Ankit"}; In practice, parallel arrays are hard to maintain. Better is to define an object with data: rollNum, marks, and name, and define arrays of these objects

Printing an Array class ArrayPrint { public static void main (String arg[]) { double numbers[] = {1,2,3}; double numbers2[] = {1,2,3,4}; String s[] = {"This", "is", "an", "array"}; printArray (s,4); } public static void printArray (String[] arr, int length) { for(int j=0;j<length;j++) System.out.println("Element "+j+"= "+arr[j]); }

Array Length Array objects have some predefined variables e.g. length String s[ ] = {"This", "is", "an", "array"}; System.out.println("Length of s: "+ s.length); Dot Syntax: If x is an object, then x.y tells you that y is some property computed for the object x.

Finding the average class Average { public static void main (String arg[]) { float dailyRainFall[] = {12.3, 13.5, 4.2, 2.4, 1.1, 0, 10.8}; int i; float average = 0; for (i=0; i<7; i++) { average += dailyRainFall[i]; } average /= 7; System.out.println (“Average rain fall: ” + average + “ mm”); }

Finding the average for (i=0; i<7; i++) { average += dailyRainFall[i]; } average /= 7; Everytime I have another day of rain, I need to change the number “7” in my program Instead, I can use for (i=0; i<dailyRainFall.length; i++) { average += dailyRainFall[i]; } average /= dailyRainFall.length;

class ArrayPrint { public static void main (String arg[]) { String s[ ] = {"This", "is", "a", "test", "array"}; printArray(s); System.out.println( "s[0] length is: "+s[0].length() ); } public static void printArray (String[] arr) { for(int j=0;j<arr.length;j++) System.out.println("Element "+j+"= "+arr[j]); } // exercise: define lengthOfStringArray (String[] s) }

Finding maximum class Maximum { public static void main (String arg[]) { int n = 100; double numbers[] = new double[n]; Initialize (numbers, n); System.out.println (“Maximum: ” + FindMax (numbers, n)); }

Finding maximum public static void Initialize (double numbers[], int length) { int i; for (i=0; i<length; i++) { numbers[i] = Math.sin(2*Math.PI/(i+1)) + Math.cos(2*Math.PI/(i+2)); }

Finding maximum public static double FindMax (double numbers[], int length) { double max = numbers[0]; int i; for (i=1; i<length; i++) { if (numbers[i] > max) { max = numbers[i]; } return max; } } // end class

Methods to return more than one parameter Want to identify both maximum marks, and also who has this maximum marks –Need to return two values from FindMax –Use a 2-element array as return type

Finding max and max index class Maximum { public static void main (String arg[]) { int n = 100; double numbers[] = new double[n]; double result[]; Initialize (numbers, n); result = FindMax (numbers, n); System.out.println ("Maximum: " + result[0] + ", Position: " + (int)result[1]); }

Finding max and max index public static void Initialize (double numbers[], int length) { int i; for (i=0; i<length; i++) { numbers[i] = Math.sin(2*Math.PI/(i+1)) + Math.cos(2*Math.PI/(i+2)); }

Finding max and max index public static double[] FindMax (double numbers[], int length) { double result[] = {numbers[0], 0}; int i; for (i=1; i<length; i++) { if (numbers[i] > result[0]) { result[0] = numbers[i]; result[1] = i; } return result; } } // end class

Array Variables Array variables are typed by their array declaration Is this legal? int[] rollNum= {6016, 6024, 6078}; int[] a = {1,2,3,4}; a = rollNum; Now the part of memory previously accessed through “a” is lost. “a” and “rollNum” now point to the same place in memory.

Passing Arrays The method findAverage() is defined thus: public static double findAverage (double[] arr) { for (int i = 1; i<arr.length; i++) { arr[0] += arr[i]; } return (arr[0] / arr.length) ; // will this work? } Q. I invoke this method on the array numbers[ ]. findAverage(numbers). Does numbers[0] remain what it was or does it change? Since method arguments in Java are passed by reference – so numbers[0] has the same address as arr[0] inside the method; and modifications to arrays inside the method are reflected outside the method

class ArrayMidsems { public static void main (String arg[]) { int[] rollNum= {6016, 6024, 6078}; double[] midsemMarks = {61.7, 54, 74.2 }; String[] name = {"Abhishek A", "Abhishek S", "Ankit"}; System.out.println (getMarks(rollNum, midsemMarks, 6024)); } public static void printArray (String[] arr, int length) { for(int j=0;j<length;j++) System.out.println("Element "+j+"= "+arr[j]); } public static double findMax (double numbers[]) { double max = numbers[0]; for (int i=1; i<numbers.length; i++) { if (numbers[i] > max) max = numbers[i]; } return max; } public static double findAverage (double[] arr) { double sum = 0; for (int i = 0; i<arr.length; i++) { sum += arr[i]; } return (sum / arr.length) ; } /* gets the marks of i-th student */ public static double getMarks (double[] marks, int i) { if (i > marks.length-1) { System.out.println ("Array index out of bounds"); return -999; } return marks[i]; } /* returns the marks for student with target rollNum */ public static double getMarks (int[] n, double[] marks, int target) { if (n.length !=marks.length) { System.out.println ("Array lengths don't match!"); return -999; } for (int j=0; j< n.length; j++) { if (n[j] == target) return marks[j]; } return -999; } } // end ArrayMidsems