Grouped Data Arrays, and Array Lists.

Slides:



Advertisements
Similar presentations
OO Programming Objectives for today: Casting Objects Introduction to Vectors The instanceof keyword.
Advertisements

5-May-15 ArrayLists. 2 ArrayList s and arrays A ArrayList is like an array of Object s Differences between arrays and ArrayList s: Arrays have special.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
CS 106 Introduction to Computer Science I 04 / 27 / 2007 Instructor: Michael Eckmann.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Arrays And ArrayLists - S. Kelly-Bootle
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
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.
CS 106 Introduction to Computer Science I 04 / 25 / 2007 Instructor: Michael Eckmann.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
ArrayList, Multidimensional Arrays
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.
Arrays Construct array: new double[10] Store in variable of type double[] double[] data = new double[10];
Copyright 2008 by Pearson Education Building Java Programs ArrayList Reading: 10.1.
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.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
The ArrayList Data Structure Standard Arrays at High Speed! More Safety, More Efficient, and Less Overhead!
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
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.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
The ArrayList Data Structure Standard Arrays at High Speed!
1 CS162: Introduction to Computer Science II Abstract Data Types.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Sixth Lecture ArrayList Abstract Class and Interface
(like an array on steroids)
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Java Generics.
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
Preconditions precondition: Something your method assumes is true at the start of its execution. Often documented as a comment on the method's header:
Method Mark and Lyubo.
Pass by Reference, const, readonly, struct
TCSS 143, Autumn 2004 Lecture Notes
Lecture 2: Implementing ArrayIntList reading:
ArrayLists.
Programming in Java Lecture 11: ArrayList
Can store many of the same kind of data together
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.
Lecture 26: Advanced List Implementation
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Arrays of Objects Fall 2012 CS2302: Programming Principles.
Java Arrays & Strings.
Can store many of the same kind of data together
Object Oriented Programming in java
slides created by Ethan Apter
ArrayLists 22-Feb-19.
Collections Framework
Can store many of the same kind of data together
Review of Previous Lesson
Suggested self-checks: Section 7.11 #1-11
ArrayLists.
ArrayLists 27-Apr-19.
slides created by Alyssa Harding
Review: libraries and packages
Generics, Stack, Queue Based on slides by Alyssa Harding
ArrayList.
CSE 143 Lecture 21 Advanced List Implementation
Arrays.
Java Coding 6 David Davenport Computer Eng. Dept.,
Presentation transcript:

Grouped Data Arrays, and Array Lists

Arrays An array is an object that can hold several values of the same type. This can be useful in a number of ways: test scores(an array of int values), class list(an array of Strings), etc. An array can hold any object type or fundamental variable type, as long as all elements are the same type.

Creating an array The brackets [ ] are the symbol for an array. They can go before or after the name of the array variable. Since arrays are objects, you also need the keyword new. So: double arrayOne[ ] = new double[20]; and: int[] arayTwo = new int[20]; are both acceptable.

Index and Elements An array holds a specific, unchangeable number of elements. The number in the second set of braces is the total number of elements in the array. The index is an int value that lets you refer to an element of the array The first index is 0 and the last is one less than the length of the array.

Assigning Values Make an array of ints called arrayThree that holds 2 elements. Now we will assign values: arrayThree[0]=5; arrayThree[1]=10; int number = arrayThree[1]; int arrayThree[] = new int[2]; What is the value of number?

Assigning at Declaration You can give an array values when you create it: double arrayFour[] = {2.5, 6.3, -.07}; The braces tell the computer that you will be supplying elements Don’t forget the first Set of brackets. How ever many elements you type, separated by commas, is the size of the array. This is the only time you do not use the keyword new

Assigning a null array If you want to make an array, but are unsure of the number of elements you want in it, you can create a null array, and give the number of elements later. boolean arrayFive[]; Some point later: arrayFive = new boolean[5]; Notice that if you want to use the array, you don’t need brackets.

Arrays as Parameters Remember that since arrays are objects, if a method mutates another reference, the actual value changes. Also, if you want to send the entire array into a method as a parameter, remember you do not use the brackets, just the name of the array reference variable.

length The array class has a public static instance variable called length. Since it is public you can reference it from other classes. Since it is static, you cannot change it. char arraySix []= new char[7]; int arSixLength = arraySix.length; arSixLength would have the value 7. Notice there are no ( )

Wrapper Class Intro Sometimes you want to use fundamental variable type values, but the situation requires an object. There are classes that are just like the fundamental types except they are objects You have had experience with them a little when you used Integer and Double.

Wrapper Class cont. Integer intOne = new Integer(5); What would be the value of: intOne == 5; ? How about intOne.compareTo(5); ? Integer class contains the methods: intValue( ) which returns the value in primitive form, compareTo(Object obj) which compares the Integer object to obj, equals(Object obj) which gives a boolean response to comparing the Integer object to obj, and toString( ) which is self-explanatory. Double has similar methods.

Array Lists ArrayList is located in java.util and must be imported. ArrayList has the advantage of shrinking and growing in number of elements. ArrayList, however, can only contain Objects. (So you would have to use wrapper classes instead of int and Double).

ArrayList Methods ArrayList has an empty constructor int size( ) returns the number of elements boolean add(Object obj) adds obj to end of list Object get(int index) returns element at index Object set(int index, Object obj) puts obj at index, returns what used to be at index. Void add(int index, Object obj) inserts obj at index, increases size by one. Object remove(int index) removes and returns object at index, decreases list size by one.

Using ArrayList What is wrong with the following: ? ArrayList listOne = new ArrayList( ); listOne.add(5); Integer intObjTwo = new Integer(5); ArrayList listTwo = new ArrayList( ); listTwo.add(0, intObjTwo); integer intOne = listTwo.get(0);