Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

1 Arrays, Strings and Collections [1] Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software Engineering.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Arrays.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
1 2-D Arrays Overview l Why do we need Multi-dimensional array l 2-D array declaration l Accessing elements of a 2-D array l Declaration using Initializer.
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
More Arrays Length, constants, and arrays of arrays By Greg Butler.
Java Unit 9: Arrays Declaring and Processing Arrays.
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.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
1 Lecture # 4. * An array is a group of contiguous or related data items that share a common name. * Each value is stored at a specific position * Position.
Arrays and Strings Introducing Arrays Declaring Arrays Creating Arrays Initializing Arrays Array of Objects Copying Arrays Multidimensional Arrays Command-Line.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
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.
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.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 8 Multidimensional Arrays Lecture.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
2D-Arrays Quratulain. Learning Objectives Two-dimensional arrays Declaration Initialization Applications.
Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 05 ARRAY 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
IN THE NAME OF ALLAH WHO IS THE MOST BENEFICENT AND MOST MERCIFUL.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
1. Define an array 1 Create reference arrays of objects in Java program 2 Initialize elements of arrays 3 Pass array to methods 4 Return array to methods.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
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.
BIT115: Introduction to Programming
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.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
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.
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
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.
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
ARRAYS Multidimensional realities Image courtesy of
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 7 Multidimensional Arrays.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Arrays Chapter 7.
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Arrays, For loop While loop Do while loop
CS-161 Computer Programming Lecture 14: Arrays I
Declaration, assignment & accessing
Introduction To Programming Information Technology , 1’st Semester
CS2011 Introduction to Programming I Arrays (I)
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
Arrays Chapter 7.
Multidimensional array
Object Oriented Programming
Arrays in Java.
C++ Array 1.
Presentation transcript:

Arrays in java Unit-1 Introduction to Java

Array There are situations where we might wish to store a group of similar type of values in a variable. Array is a memory space allocated, which can store mutiple values of same data type, in contiguous locations. This memory space, which can be perceived to have many logical contiguous locations, can be accessed with a common name. A specific element in an array is accessed by the use of a subscript or index used in brackets, along with the name of the array. For example, marks[5]would mean marks of 5th student. While the complete set of values is called an array, the individual values are known as elements. Arrays can be two types: one dimensional array multi-dimensional array

1-D Arrays In one-dimensional array, a single subscript or index is used, where each index value refers to indivdidual array element. The indexation will start from 0 and will go up to n-1, i.e. the first value of the array will have an index of 0 and the last value will have an index of n–1, where ŋ is the number of elements in the array. So, if an array named marks has been declared to store the marks of 5 students, the computer reserves five contiguous locations in the memory, as shown below

Creation of Arrays Creating an array, similar to an object creation, can inherently involve three steps: Declaring an array Creating memory locations Initializing/assigning values to an array

Declaring an array Declaring an array is same as declaring a normal variable except that you must use a set of square brackets with the variable type. There can betwo ways in which an array can be declared. type arrayname[ ]; type[ ] arrayname; For e.g. int marks[ ]; or int[ ] marks;

Creating memory locations An array is more complex than a normal variable, so we have to assign memory to the array when we declare it. You assign memory to an array by specifying its size. Interestingly, our same old new operator helps in doing the job, just as shown below: Arrayname = new type [size]; So, allocating space and size for the array named as marks can be done as, marks = new int[5];

initializing/assigning values to an array Assignment of values to an array, which can also be termed as initialization of array, can be done as follows, Arrayname[index] = value; The creation of list of marks to be assigned in array, named as marks has already been shown in the section above. Here is an example of how to set the values for an array of 5 marks.

Setting Values in An Array public class Array { public static void main(String[] args) { int[] marks = new int[5]; marks[0] = 60; marks[1] = 58; marks[2] = 50; marks[3] = 78; marks[4] = 89; for(int i=0; i<4; i++) { System.out.println(marks[i]);} Alternatively you can also use: int marks[] = {60, 58, 50, 78, 89}

Sorting Arrays class SortArray { public static void main(String[] args) { int[] marks = {3, 5, 1, 2, 4}; int temp, n; n = marks.length; System.out.println(“The list of marks is: “); for(int i=0; i<n; i++) { System.out.println(marks[i]);} for (int i = 0; i<n; i++){ for (int j = i+1; j < 4; j++){ if (marks[i] < marks[j]) { temp = marks[i]; marks[i] = marks[j]; marks[j] = temp; }}} System.out.println(“List of marks sorted in decending order is:” ); for (int i=0; i<n; i++) { System.out.println(marks[i]); }}}

2-D Arrays Sometimes values can be conceptualized in the form of table that is in the form of rows and columns. Suppose we want to store the marks of different subjects. We can’t store it in a one-dimensional array.

2 D Arrays (contd.) Following statements create a two-dimensional array, named as marks, which would have 4 rows and 5 columns

2 D Arrays (contd.) This statement just allocates a 4 × 5 array and assigns the reference to array variable marks. The first subscript inside the square bracket signifies the number of rows in the table or matrix and the second subscript stands for the number of columns. This 4 × 5 table can store 20 values altogether. Its values might be stored in contiguous locations in the memory, but logically, the stored values would be treated as if they are stored in a 4 × 5 matrix. The following table shows how the marks array is conceptually placed in the memory by the above array creation statement

2 D Arrays (contd.) Like a one-dimensional array, two-dimensional array may be initialized with values, at the time its creation. For example, int marks[2][4] = {2, 3, 6, 0, 9, 3, 3, 2}; This declaration shows that the first two rows of a 2 × 4 matrix have been initialized by the values shown in the list above. It can also be written as, int marks[ ][ ] = {(2, 3, 6, 0), (9, 3, 3, 2)};

2 D Arrays (Example) class MatrixMul { public static void main(String args[]) { int array[][] = {(3,7),(6,9)}; int array1[][] = {(5,4),(3,6)}; int array2[][] = new int[2][2]; int x = array.length; System.out.println(“Matrix 1: “); for (int i=0; i<x; i++) {/*Line 9*/ for (int j=0; j<=x; j++) { System.out.println(“ “+array[i][j]); } System.out.println(); }

Using for-each with arrays The enhanced for loop, i.e. for-each was introduced in Java 5 to enhance iterations over arrays and other collections easier. The format of for-each is as follows: for (type var : arr){ // Body of loop } For example, to calculate the sum of the elements of the array, for-each can be used as follows. int[] arr= {2,3,4,5,6}; int sum = 0; for( int a : arr) // a gets successively each value in arr { sum += a; }