Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is.

Slides:



Advertisements
Similar presentations
Chapter 8: Arrays.
Advertisements

Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Computer Science 209 Software Development Equality and Comparisons.
1 Various Methods of Populating Arrays Randomly generated integers.
CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
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.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
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.
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 One-Dimensional (1-D) Array Overview l Why do we need 1-D array l 1-D array declaration and Initialization l Accessing elements of a 1-D array l Passing.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
ECE122 L13: Arrays of Objects March 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 13 Arrays of Objects.
1 Lecture Today’s topic Arrays Reading for this Lecture: –Chaper 11.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
1 Search & Sorting Using Java API Searching and Sorting Using Standard Classes  Searching data other than primitive type.  Comparable interface.  Implementing.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Two Ways to Store Data in a File Text format Binary format.
1 ArrayList  Array’s are limited because we need to know the size before we use them.  An ArrayList is an extension of an array that grows and shrinks.
AP CS Workshop ArrayList It is very common for applications to require us to store a large amount of data. Array lists store large amounts of data.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Computer Science 210 Computer Organization Arrays.
© 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.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS.
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.
Introduction to Collections Arrays. Collections Collections allow us to treat a group of values as one collective entity. The array is a collection of.
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.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
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.
1 On (computational) simplicity We are trying to teach not just Java, but how to think about problem solving. Computer science has its field called computational.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
Question 1a) What is printed by the following Java program? int s; int r; int i; int [] x = {4, 8, 2, -9, 6}; s = 1; r = 0; i = x.length - 1; while (i.
Arrays.
1 BUILDING JAVA PROGRAMS CHAPTER 7.2 ARRAY TRAVERSAL ALGORITHMS.
Java Part I By Wen Fei, HAO. Program Structure public class ClassName { public static void main(String[] args) { program statements } user defined methods.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays days until the AP Computer Science test.
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.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
Arrays and Array Lists CS 21a. Problem 1: Reversing Input Problem: Read in three numbers and then print out the numbers in reverse order Straightforward.
The ArrayList Data Structure The Most Important Things to Review.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 4 GEORGE KOUTSOGIANNAKIS Copyright: 2016 Illinois Institute of Technology/George Koutsogiannakis 1.
02/09/2005 Introduction to Programming with Java, for Beginners Arrays (of primitives)
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
CMSC 150 ARRAYS CS 150: Fri 10 Feb Motivation  Consider a list of your contact addresses: String Zero = String.
Arrays and Sorting. Process Open a file that contains integers, one per line. Read each line, convert to short and store each into an array Sort the array.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 7 Arrays…. 7-2 Arrays An array is an ordered list of values An array of size N is indexed from.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Introduction to programming in java
Arrays.
Lecture 3 Linear Search and Binary Search ArrayLists
Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Arrays November 8, 2017.
L3. Necessary Java Programming Techniques
L3. Necessary Java Programming Techniques
Building Java Programs
Presentation transcript:

Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is a reference * an array must be instantiated (created) * an array must be instantiated (created)

An array can store primitive values…………. int[] myarr; // array reference myarr = new int[5]; myarr myarr[0] myarr[4]

int [] myarr = new int[5]; //initialize array String fiveletterword = JOptionPane.showInputDialog(“Enter 5 letter word”); for (int strpos=0; strpos < 5; strpos++) myarr[strpos] = fiveletterword.charAt(strpos); //what’s going on here //print out the ASCII values in the array String temp = “”; for (int index=0; index < 5; index ++) temp = temp + “ “ + myarr[index]; System.out.println(temp); //what is printed, numbers or chars? // using length field of an array for (int index = 0; index < myarr.length; index ++) myarr[index] = 100;

Can also create an array object by providing initial values: double[] values = { 5.5, 6.1, 0.0, 6.1, 4.3, 7.1}; values is an array of length 6, with legal indices 0 to 5

An array can be declared to store any type … class types too. import java.io; public class demo{ public static void main (String [] xx) throws IOException { String[] groceries = new String [100]; //each element of groceries is a reference to an object FileReader instream = new FileReader(“list.txt”); BufferedReader in = new BufferedReader(instream); String item = in.readLine(); int index = 0; while (item != null) { groceries[index] = item; index++; item = in.readLine(); }

bagels coffee FileReader instream = new FileReader(“list.txt”); BufferedReader in = new BufferedReader(instream); String item; int index = 0; while ((item = in.readLine() )!= null) groceries[index++] = item; //alternative loop implementationtea groceries String objects……

Assuming the array groceries.. What would the following print to the screen……. for (int i= 0; i < groceries.length; i++) temp = temp + “ “ + groceries[i]; System.out.println(temp);

for (int i= 0; i < numItems; i++) temp = temp + “ “ + groceries[i]; System.out.println(temp); coffee tea bagels null null null null null null null (90 more times) String item = in.readLine(); //code which stored values in array int index = 0; int numItems = 0; while (item != null) { groceries[index] = item; index++; item = in.readLine(); } int numItems = index;

Each item of the array groceries is a String object, and can be used to call String methods…… for (i=0; i < numItems; i++) groceries[i] = groceries[i].toUpperCase(); What would happen if I replaced numItems with groceries.length ??

The java.util package provides a class named Arrays. This class is full of STATIC METHODS which work with array parameters…………

Code which stores, sorts and then searches thru data is easy……. f or (index = 0; index < numItems; index++) temp = temp + " " + groceries[index]; System.out.println("groceries are" + temp); String [] groceries2 = new String[numItems]; System.arraycopy(groceries,0, groceries2, 0, numItems); Arrays.sort(groceries2); //must sort and search full array while ((item=JOptionPane.showInputDialog("Enter item to look for ")) != null){ index = Arrays.binarySearch(groceries2,item); if (index >=0 ) JOptionPane.showMessageDialog(null, item + " is there"); else JOptionPane.showMessageDialog(null, item + " is not there"); }

If I had an array of Dog objects, would I be able to sort and search ???????????? need to know about interfaces………………..