Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Advertisements

Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
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 Brooks/Cole Chapter 7 Strings and Characters.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Strings and Arrays The objectives of this chapter are:  To discuss the String class and some of its methods  To discuss the creation and use of Arrays.
Programming 2 CS112- Lab 2 Java
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
J.43 ARRAYS  A Java array is an Object that holds an ordered collection of elements.  Components of an array can be primitive types or may reference.
Java Syntax Primitive data types Operators Control statements.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Arrays CS Feb Announcements Exam 1 Grades on Blackboard Project 2 scores: end of Class Project 4, due date:20 th Feb –Snakes & Ladders Game.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 5: Objects Sanjay Goel University.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
1 Lecture # 4. * An array is a group of contiguous or related data items that share a common name. * Each value is stored at a specific position * Position.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
From C++ to Java A whirlwind tour of Java for C++ programmers.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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.
© 2007 Lawrenceville Press Slide 1 Chapter 10 Arrays  Can store many of the same kind of data together  Allows a collection of related values to be stored.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Working with string In java Four classes are provided to work with String:1) String 2)String Buffer 3)String Tokenizer 4)String Builder Note: An object.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
Array Declarations Arrays contain a fixed number of variables of identical type Array declaration and allocation are separate operations Declaration examples:
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
1 Unit-2 Arrays, Strings and Collections. 2 Arrays - Introduction An array is a group of contiguous or related data items that share a common name. Used.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
Chapter 8: Loops, Arrays, Strings Loop statements –do –while –for Arrays –declaration, allocation, initialization, access –multi-dimensional –heterogeneous.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Java Programming Language Lecture27- An Introduction.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Strings, Characters and Regular Expressions
Array, Strings and Vectors
Multiple variables can be created in one declaration
Java Review: Reference Types
Primitive Types Vs. Reference Types, Strings, Enumerations
Chapter 7: Strings and Characters
MSIS 655 Advanced Business Applications Programming
In Java, strings are objects that belong to class java.lang.String .
Presentation transcript:

Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel University at Albany, SUNY Fall 2004

Sanjay Goel, School of Business, University at Albany, SUNY 2 Recap Strings Arrays –Creation and Access –Copying & assigning arrays –Storage & Indexing –Multidimensional Array Examples Outline for the Class Arrays

Sanjay Goel, School of Business, University at Albany, SUNY 3 Strings

Sanjay Goel, School of Business, University at Albany, SUNY 4 Objects and Classes Standard Classes: String String class represents character strings All string literals in Java programs are implemented as instances of this class, e.g. “abc” String class includes methods for –Examining individual characters of the sequence –Comparing strings –Searching strings –Extracting substrings –Creating a copy of a string with upper or lower case translation.

Sanjay Goel, School of Business, University at Albany, SUNY 5 Objects and Classes String Methods String class has following methods –boolean equals(Object anObject) Compares this string with another –int length() Gets length of string –char charAt(int index) Returns char at index pos. in string –int compareTo(String str) Returns an integer based on lexigographic order –int indexOf(in ch) Gets position of character in string (-1 if not present) –int indexOf(String str) Gets position of first letter of str in the string –String concat(String str) Concatenates two strings and returns –String toLowerCase() Converts to lower case –String toUpperCase() Converts to upper case –char[] toCharArray() Returns character array –static String valueof(type prim) Converts primitive to string.

Sanjay Goel, School of Business, University at Albany, SUNY 6 Objects and Classes Standard Classes: Palindrome static boolean isPalindrome(String s) { int left = 0; int right = s.length() – 1; while (left < right) { if (s.charAt(left) != s.charAt(right)) return false; left++; right--; } return true; }

Sanjay Goel, School of Business, University at Albany, SUNY 7 Creating Classes The equals Method Definition –Returns true if the strings are the same and false if they differ. –As soon as a character in one string is not equal to the corresponding character in the second string, the comparison is terminated and the condition returns to false. Syntax Example –(strName.equals(strInput)) (strName.equals(“Bill”))

Sanjay Goel, School of Business, University at Albany, SUNY 8 Creating Classes The equalsIgnoreCase Method Definition –Is similar to the equals method but does not differentiate between uppercase and lowercase. Syntax Example –String strName = new String(“Joan”) ; String strName2 = new String(“JOAN”) ;

Sanjay Goel, School of Business, University at Albany, SUNY 9 Creating Classes The compareTo Method Definition –Used to determine less than or greater than. –Returns an integer with one of three possible values. Syntax Example –String strWord = new String(“Hope”) ; String strWord2 = new String(“Hopeless”) ; //Compare the strings if (strWord.compareTo(strWord2 < 0) //Display a message - - What will it be?

Sanjay Goel, School of Business, University at Albany, SUNY 10 Arrays

Sanjay Goel, School of Business, University at Albany, SUNY 11 Arrays Introduction When do we need arrays? –When we have large data sets of the same type –e.g. test scores of the students or prime numbers between 0 and 100. –It is cumbersome to define a separate variable for each value. –Allows you to define one single variable and just put multiple values in An array in Java is a container that holds a related group of values of the same type. –The elements in an array are ordered are numbered 0 through n-1 –where n is the number of individual elements in the array. The position of an element in an array is called index of the array

Sanjay Goel, School of Business, University at Albany, SUNY 12 Arrays Storage in Memory Before we even start let us understand how memory is organized and how data is stored in memory. –Each location in memory has a unique address. Data:

Sanjay Goel, School of Business, University at Albany, SUNY 13 Arrays Memory Pointers This means that the memory location where the array points does not contain array data rather it contains a reference or a pointer to the actual array data. –When you declare an array you just create a pointer which points to nothing. –To find the address in memory of a specific element, the computer can add the element’s index to the address of the location of the first element.

Sanjay Goel, School of Business, University at Albany, SUNY 14 Arrays Array Declaration and Memory Allocation This is a two step process: Array Declaration –You declare array variables just like any other variable by specifying the type followed by the name of the variable. –variableType [] variableName; –int [] myArray; Memory Allocation –To actually create an array you have to use new. arrayName = new arrayType[length] –length is an integer expression –e.g. myArray = new int[10]

Sanjay Goel, School of Business, University at Albany, SUNY 15 Arrays Array Length You can do both things in a single step –arrayType arrayName = new arrayType[length] –Once an array is created it has a fixed size. –An array can be reassigned to point to a different array that has a different size

Sanjay Goel, School of Business, University at Albany, SUNY 16 Arrays Array Indexing Array Indexing –Once you declare an array. Each element can be accessed by using an index variable of integer type. The indexing is from 0 to n-1 for an array of length n. –arrayName[expr] This expression is any integer expression –i th element of the array is represented as arrayName[i-1]

Sanjay Goel, School of Business, University at Albany, SUNY 17 Arrays Assigning Arrays int [] a = new int[10]; a = new int[20]; or int [] a1 = {10, 20, 30} int [] a2 = {1, 2, 3, 4, 5} a1 = a2; // Now both a1 and a2 are pointing to same variable

Sanjay Goel, School of Business, University at Albany, SUNY 18 Arrays Length of an Array The length of the array is stored in a variable called length and can be accessed as: arrayName.length e.g. a.length; for (int i = 0; i < a.length. i++) { } If you index past the end of the array you get an IndexOutOfBoundsException error.

Sanjay Goel, School of Business, University at Albany, SUNY 19 Arrays Passing arrays to methods Java passes parameters by value. Arrays are reference types i.e. they store the address of the array location So when we pass arrays as arguments a copy of the reference value (address) is passed to the method. Two Scenarios 1.The contents of the array are modified a.The main program sees the changes to the array  show using stack 2.The array is assigned to another array in the method a.No change happens in the calling method  show using stack

Sanjay Goel, School of Business, University at Albany, SUNY 20 Arrays Copying Arrays You need to copy arrays element by element rather than just assigning one array to another. By just assigning one array name to another you are just copying pointers without copying the array. If you want to create a new array from an old array you need to create a new array and assign all the values from the old array to the new array, e.g. static int[] duplicate(int[] a) { int[] theCopy = new int[a.length]; for(int I = 0; I < a.length; I++) { theCopy[I] = a[I]; } return theCopy; } a1 = duplicate(a2) Cloning: For one-dim arrays java provides a cloning mechanism –i.e. a1 = (int[])a2.clone(); // built-in array copy

Sanjay Goel, School of Business, University at Albany, SUNY 21 Arrays Types and Arrays Similar to integer arrays you can have arrays in other primitive types and also classes. Primitive Types –double [] d is an array of doubles –char [] c is an array of characters Non-Primitive Types –String[] args is an array of Strings –Point[] points is an array of points Show the example of sieve of eratosthenes Show the example of character count

Sanjay Goel, School of Business, University at Albany, SUNY 22 Arrays Multidimensional Array Just like single dimensional arrays we have multi-dimensional arrays int[] a1;Row int[][] a2;Matrix int[][][] a3;3D Grid Declaring 2D arrays int[][] a2 = new int[expr1][expr2]; Initializing 2D arrays int[][] a = {{1,2},{3,4},{5,6}}; // 3x2 int[][] b = {{1,2,3}, {4,5,6}}; // 2x3 int[][] c = {{1,2,3,4,5,6}}; // 1x3 int[][] ragged = {{1,2}, {3,4,5}, {6}} // 3 rows each with different # of elements

Sanjay Goel, School of Business, University at Albany, SUNY 23 Arrays Examples Sum of an array two different ways manually without using loops Minimum and maximum of an array Sieve of Eratosthenes Example of Character Count Sorting of arrays (selection sort) Searching an ordered array Algorithm Complexity –Sorting and Searching