Arrays in JAVA Visit for more Learning Resources.

Slides:



Advertisements
Similar presentations
Arrays in JAVA CS 340 Software Design Muhammad Talha.
Advertisements

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.
Java: How to Program Arrays and ArrayLists Summary Yingcai Xiao.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
1 Course Lectures Available on line:
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Console Input. So far… All the inputs for our programs have been hard-coded in the main method or inputted using the dialog boxes of BlueJ It’s time to.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
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.
Pemrograman Dasar Arrays PTIIK - UB. Arrays  An array is a container object that holds a fixed number of values of a single type.  The length of an.
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.
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.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
1 Variables. 2 Receipt example What's bad about the following code? public class Receipt { public static void main(String[] args) { // Calculate total.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Lab 8. Declaring and Creating Arrays in One Step datatype[] arrayRefVar = new datatype[arraySize]; double[] myList = new double[10];
Lecture 07. Do not have to create an array while declaring array variable [] variable_name; int [] prime; int prime[]; Both syntaxes are equivalent No.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Array and String.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
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.
IAS 1313: OBJECT ORIENTED PROGRAMMING Week 3: Data Type, Control Structure and Array Prepared by: Mrs Sivabalan1.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. System.out.println(“Please enter grade.
Elementary Programming
Two Dimensional Array Mr. Jacobs.
Chapter 7 Part 1 Edited by JJ Shepherd
Fifth Lecture Arrays and Lists
Lecture Note Set 1 Thursday 12-May-05
Motivations Thus far, you have used one-dimensional arrays to model linear collections of elements. You can use a two-dimensional array to represent a.
Java Arrays. Array Object An array :a container object holds a fixed number of values of a single type. The length of an array is established when the.
SELECTION STATEMENTS (1)
Object Oriented Programming
Chapter 8: Collections: Arrays
Arrays An Array is an ordered collection of variables
Yong Choi School of Business CSU, Bakersfield
Pass by Reference, const, readonly, struct
Chapter 6 Arrays Solution Opening Problem
Chapter 8 Multi-Dimensional Arrays
Java Language Basics.
Introducing Arrays Array is a data structure that represents a collection of the same types of data.
EKT150 : Computer Programming
Introduction To Programming Information Technology , 1’st Semester
Java Arrays & Strings.
BIT115: Introduction to Programming
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
CS2011 Introduction to Programming I Arrays (I)
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
Arrays.
Announcements Lab 6 was due today Lab 7 assigned this Friday
OBJECT ORIENTED PROGRAMMING II LECTURE 13_2 GEORGE KOUTSOGIANNAKIS
Single-Dimensional Arrays chapter6
Object Oriented Programming
Arrays in Java.
Chapter 8 Multidimensional Arrays
Python Basics with Jupyter Notebook
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
Arrays Arrays A few types Structures of related data items
Arrays in Java Prepare 1 whole yellow paper.
C++ Array 1.
Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7.
Presentation transcript:

Arrays in JAVA Visit for more Learning Resources

Array is collection of related data items Creating an array Declare an array Create memory location Putting values to memory locations

Declaring an Array Variable Do not have to create an array while declaring array variable <type> [ ] variable_name; Double[ ] myList; double myList[ ]; Both syntaxes are equivalent No memory allocation at this point

Defining an Array Define an array as follows: variable_name=new <type>[arraySize]; Number = new int[5]; Mylist = new int[10]; It creates an array using new dataType[arraySize]; It assigns the reference of the newly created array to the variable variable_name. dataType arrayname[ ] = {list of values}; Int a [ ]={1,2,3,4,5,6,7,}; Array index starts from 0 to arraySize-1; int is of 4 bytes, total space=4*10=40 bytes Declaring and defining in the same statement: Double [ ] mylist = new double[10];

Creating arrays cntd...

What happens if we define diffrent type… Int[ ] a=new long[20]; incompatible types found: long[] required: int[] The right hand side defines an array, and thus the array variable should refer to the same type of array Example: int prime[100]; error=> ']' expected long primes[20]; The C++ style is not permitted in JAVA syntax long[] primes = new long[20]; primes[25]=33; Runtime Error:Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException

Array Size through Input …. BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); String inData; int num; System.out.println("Enter a Size for Array:"); inData = stdin.readLine(); num = Integer.parseInt( inData ); // convert inData to int long[] primes = new long[num]; System.out.println(“Array Length=”+primes.length); SAMPLE RUN: Enter a Size for Array: 4 Array Length=4

Example for array public class TestArray { public static void main(String[] args) { double[] myList = {1.9, 2.9, 3.4, 3.5}; // Print all the array elements for (double element: myList) { System.out.println(element); } Otput: 1.9 2.9 3.4 3.5

Reusing Array Variables int[] primes=new int[10]; …… primes=new int[50]; Previous array will be discarded Cannot alter the type of array

Demonstration long[] primes = new long[20]; primes[0] = 2; System.out.println(primes[0]); System.out.println(primes[1]); Output: 2 3

Array Length Refer to array length using length() method A data member of array object array_variable_name.length for(int k=0; k<primes.length;k++) Sample Code: long[ ] primes = new long[20]; System.out.println(primes.length); Output: 20 If number of elements in the array are changed, JAVA will automatically change the length attribute!

Sample Program class MinArray { public static void main ( String[] args ) int[] array = { 20, 19, 1, 5, 71, 27, 19, 95 } ; int min=array[0]; // initialize the current minimum for ( int index=0; index < array.length; index++ ) if ( array[ index ] < min ) min = array[ index ] ; System.out.println("The minimum of this array is: " + min ); } *Program taken from: http://chortle.ccsu.edu/CS151/Notes/chap47/ch47_10.html

Two dimenssional array Representing 2D arrays Int myarray[][]; Myarray = new int[3][4]; Int myarray [][] = new int[3][4]; Example Int myarray[2][3]={0,0,0,1,1,1}; 2 columns and 3 rows For more detail contact us