Java – Part II Lecture Notes 4. Arrays An array is a data structure that groups and organizes data l Array is a list of values (int, double, aggregates)

Slides:



Advertisements
Similar presentations
1 Various Methods of Populating Arrays Randomly generated integers.
Advertisements

Arrays and ArrayLists Ananda Gunawardena. Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages.
Lecture 05 - Arrays. Introduction useful and powerful aggregate data structure Arrays allow us to store arbitrary sized sequences of primitive values.
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
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 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
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.
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.
25-Jun-15 Vectors. 2 Vectors and arrays A Vector is like an array of Object s Differences between arrays and Vector s: Arrays have special syntax; Vector.
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 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Multiple-Subscripted Array
Vectors. Vectors and arrays A Vector is like an array of Object s Differences between arrays and Vector s: –Arrays have special syntax; Vector s don’t.
Arrays in Java Selim Aksoy Bilkent University Department of Computer Engineering
Arrays. 2 The array data structure An array is an indexed sequence of components Typically, the array occupies sequential storage locations The length.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Introduction to C Programming CE Lecture 9 Data Structures Arrays.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Programming Fundamentals I (COSC-1336), Lecture 8 (prepared after Chapter 7 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 8.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1.00/ Lecture 8 Arrays and Vectors. Arrays-1 Arrays are a simple data structure Arrays store a set of values of the same type – Built-in types.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
C++ Lecture 3 Monday, 14 July Arrays, Pointers, and Strings l Use of array in C++, multi- dimensional array, array argument passing l Pointers l.
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
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 and Strings. Why? Consider a class of 30 students, each has a score for hw1  Do we want to have 30 variables with different names?  Would it.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
Title Slid CSC 444 Java Programming Arrays By Ralph B. Bisland, Jr.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 ArrayLists Section 9.9.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Array Declarations Arrays contain a fixed number of variables of identical type Array declaration and allocation are separate operations Declaration examples:
IT259 Foundation of Programming Using Java Unit 9 Seminar : (Chapter 8 ) Instructor : Vladimir Gubanov, PhD
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.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
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.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
1 Why do we need arrays? Problem - Input 5 test scores => int test1,test2,test3,test4,test5 100 test scores? 10,000 employees? A structured data type is.
Computer Programming BCT 1113
Chapter 8: Collections: Arrays
Programming in Java Lecture 11: ArrayList
Review of Arrays and Pointers
Introduction To Programming Information Technology , 1’st Semester
Java Arrays & Strings.
The Vector Class An object of class Vector is similar to an array in that it stores multiple values However, a vector only stores objects does not have.
Data Structures (CS212D) Week # 2: Arrays.
Arrays Week 2.
ArrayLists 22-Feb-19.
ArrayLists 27-Apr-19.
Presentation transcript:

Java – Part II Lecture Notes 4

Arrays An array is a data structure that groups and organizes data l Array is a list of values (int, double, aggregates) l The number corresponding to each position is called an index or subscript Index = 0 Value = 12 Index = 2 Value = 16 Index = 1 Value = 10

Declaring and using Arrays Arrays are objects Int [] height = new int[11] New operator allocates memory space to save values The type of the array is int [] Eg: Random x = new Random(); final int LIMIT = 15; int [] list = new int[LIMIT] for (int I=0; I<LIMIT;I++) { list[I] = x.nextInt(LIMIT);} for (int I=0; I<LIMIT;I++) { System.out.print(list[I]+”\t”);}

Declaring and using Arrays Two ways to declare arrays: int [] grades; int grades[]; No difference as far as compiler is concerned First one is more consistent with type declarations int [] A, B, C; int A[], B, C[];

Interactively Read a set of numbers into an Array import javax.swing.JOptionPane; boolean done = false; final int LIMIT = 15; int [] list = new int[LIMIT]; int index = 0; Int num; While (true) { num =Integer.parseInt( JOptionPane.showInputDialog("Enter an integer")); if (I==LIMIT-1 || num==-999) break; else list[I++] = num; }

An Array Example final int NUMCHARS = 26; String line = JOptionPane.showInputDialog("Enter a sentence"); char current; int other= 0; int [] upper = new int[NUMCHARS]; For (int I=0; I<line.length(); I++) { current = line.charAt(I); if (current >= ‘A’ && current <= ‘Z’) upper[current-’A’]++; } What does above program do?

More on Arrays Array Intializer int [] list = {1,2,3,4}; Array as Parameter An entire array can be passed as a parameter to a method A copy of the reference to the array is passed A method can change array elements permanently A method cannot change the reference itself

More on Arrays Array of String Objects String [] words = new String[10]; Creates an array that holds references to String objects Reversing an Array for (int I=0; I<SIZE/2;I++) { int temp = list[I]; list[I] = list[SIZE-I-1]; list[SIZE-I-1]=temp; }

Two Dimensional Arrays A Table (Grid) of Rows and Columns Uses Two Indexes to refer to an element Two Dimensional Array(or Matrix) is an array of Arrays Eg: int [][] Table = new int[5][10]; for (int I=0; I<Rows; I++) for (int j=0;j<Cols;j++) Table[I][j]=I+j; for (int I=0;I<Table.length; I++) for (int j=0;j<Table[I].length;j++) System.out.print(Table[I][j]);

Two Dimensional Arrays ctd.. Int [][] Scores = {{2,3,3},{2,2,2}}; Defines a 2 by 3 matrix (I.e 2 rows and 3 columns) Finding Row Sum of Row 0 (first row) for (int j=0; j<Scores[0].length;j++) sum += Scores[0][j]; Finding Column Sum of Column 2 (third column) for (int i=0; i<Scores.length;i++) sum += Scores[i][2];

The Vector Class import java.util.* Vector is like an array Stores multiple values by an index Array size is fixed after declaration Vector object can grow dynamically as needed Vector objects maintain list of references to an object class Some Vector Methods Vector () - default constructor (size =0) void addElement(Object obj); void insertElementAt(Object obj, int index) void setElementAt(Object obj, int index) Object remove(int index)

Some Vector Methods ctd.. boolean removeElement(Object obj) void removeElementAt(int index) void clear() boolean contains(Object obj) int indexOf(Object obj) Object elementAt(int index) boolean isEmpty() int size();

A Vector Example Vector Band = new Vector(); Band.addElement(“Guna”); Band.addElement(“Mary”); Band.addElement(“Mo”); // print the entire band as a vector [ …..] System.out.println(Band); Band.removeElement(“Mo”); // insert (by pushing others to right, at index 2) Band.insertElementAt(“Ringo”, 2);

Conclusion One Dimensional Arrays(vectors) Have many Applications. Read a Set of Data Sort, Search, Modify, insert, Delete and many more Two Dimensional Arrays have many applications Computer Graphics Table implementation and many more