Java Arrays [Lists] in 10 Minutes. Declaring an array (and its types) int[] myIntArray; double[] myDoubleArray; String[] myStrings; //What’s the pattern?

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 Array and Collections.
Advertisements

Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
Basic Algorithms on Arrays. Learning Objectives Arrays are useful for storing data in a linear structure We learn how to process data stored in an array.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 14, 2005.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
Programming with Collections Collections in Java Using Arrays Week 9.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 Arrays Chapter 6.
Arrays. Arrays  When a value is to be used in a program, a variable is declared to hold that value  What about storing the results of exams for a large.
Lecture 7: Arrays Yoni Fridman 7/9/01 7/9/01. OutlineOutline ä Back to last lecture – using the debugger ä What are arrays? ä Creating arrays ä Using.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
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.
COMP 110 Introduction to Programming Mr. Joshua Stough October 24, 2007.
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.
Introduction to Computers and Programming Class 21 Arrays Professor Avi Rosenfeld.
COMP 14 Introduction to Programming Miguel A. Otaduy June 1, 2004.
Java Unit 9: Arrays Declaring and Processing Arrays.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
2 dimensional arrays Steven Wood ©2005. Arrays dimensions Java allows arrays with many subscripts 2-D examples Chess board Excel spreadsheet.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
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.
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.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: 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.
Chapter 8: Arrays.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
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.
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
Arrays BCIS 3680 Enterprise Programming. Overview 2  Array terminology  Creating arrays  Declaring and instantiating an array  Assigning value to.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Arrays. An array is a group of like-typed variables that are referred to by a common name. Arrays of any type can be created and may have one or more.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
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.
For Loop Tips And Tricks
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
1 BUILDING JAVA PROGRAMS CHAPTER 7.2 ARRAY TRAVERSAL ALGORITHMS.
Chapter 8 P 1 Arrays and Grids Single-dimension arrays Definition An array is a sequence of elements all referred to with a common name. Other terms: table,
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Arrays.
Arrays An array is an indexed data structure which is used to store data elements of the same data type. An array is an indexed data structure which is.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
Chapter 5: ARRAYS ARRAYS. Why Do We Need Arrays? Java Programming: From Problem Analysis to Program Design, 4e 2  We want to write a Java program that.
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];
Week 6 - Friday.  What did we talk about last time?  Loop examples.
CSI 3125, Preliminaries, page 1 Arrays. CSI 3125, Preliminaries, page 2 Arrays Group of related typed variables that referred to a common name Each data.
02/09/2005 Introduction to Programming with Java, for Beginners Arrays (of primitives)
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
Introduction to programming in java Lecture 21 Arrays – Part 1.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline 1 Introduction 2 Arrays 3Declaring Arrays 4Processing Array Contents 5 Multiple-Subscripted.
Array in C# Array in C# RIHS Arshad Khan
A simple way to organize data
Arrays and Collections
CS2011 Introduction to Programming I Arrays (I)
Presentation transcript:

Java Arrays [Lists] in 10 Minutes

Declaring an array (and its types) int[] myIntArray; double[] myDoubleArray; String[] myStrings; //What’s the pattern? //dataType[] arrayName; //Note: the arrays don’t really exist yet!

Initializing an array int[] myIntArray = new int[4]; double[] myDoubleArray = new double[5]; String[] myStrings = new String[3]; //What’s the pattern? //dataType[] arrayName = new dataType[size]; //Now we’ve allocated new space for the arrays //Each array element has a default 0 value

Explicit initialization //or we can explicitly initialize the arrays int[] myIntArray = { 4, 2, 6, 8 }; double[] myDoubleArray = { 1.0, 2.3, 6.3, 7.5, 0.1 }; String[] myStrings = {“lol”,“omg”,“ROTFLSHMSFOAIDMT”} //Note: use { } and separate elements with commas

Size/length of an array int[] myIntArray = { 4, 2, 6, 8 }; System.out.println( myIntArray.length ); //prints 4 //Note: length has no () with arrays!!!!

Accessing an array element int[] myIntArray = { 4, 2, 6, 8 }; System.out.print( myIntArray[0] ); //prints 4 System.out.print( myIntArray[2] ); //prints 6 System.out.print( myIntArray[myIntArray.length-1] ); //prints 8 //Each element in the array has an index //Indices start at 0 and end at length-1

Changing/setting an array element int[] myIntArray = { 4, 2, 6, 8 }; myIntArray[0] = 9; //myIntArray now has [9, 2, 6, 8] myIntArray[1] = myIntArray[2]; //myIntArray now has [9, 6, 6, 8]

Loops and arrays //Example of linear traversal of array int[] myIntArray = { 4, 2, 6, 8 }; for (int i = 0; i < myIntArray.length; i++) { System.out.println(myIntArray[i]); } //Try out on your own the same loop and print out all the words in: String[] myStrings = {“lol”,“omg”,“ROTFLSHMSFOAIDMT”}

Initialize array elements with a loop //Traverse the array and set each element to 3 int[] myIntArray = { 4, 2, 6, 8 }; for (int i = 0; i < myIntArray.length; i++) { myIntArray[i] = 3; }

Reference variables  Array variables are actually references to the arrays Example: int[] myIntArray = new int[4]; int[] array2 = myIntArray; //How many arrays are there? (Hint: How many times did we say “new”? //How many array references are there? (Hint: How many times did we say “int[]”?

Reference Variables cont’d int[] myIntArray = { 4, 2, 6, 8 }; int[] array2 = myIntArray; array2[0] = 0; System.out.print(myIntArray[0]); //prints 0 //myIntArray and array2 “refer” to the same array