Upcoming SlideShare
×

# Arrays in Java

2,983
-1

Published on

3 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total Views
2,983
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
253
0
Likes
3
Embeds 0
No embeds

No notes for slide

### Arrays in Java

1. 1. ARRAYS in JAVA
2. 2. TOPICS TO COVER:--  Array declaration and use.  One-Dimensional Arrays.  Passing arrays and array elements as parameters  Arrays of objects  Searching an array  Sorting elements in an array
3. 3. ARRAYS An array is group of like-typed variables that are referred to by a common name.The entire array Each value has a numeric indexhas a single name 0 1 2 3 4 5 6 scores 50.5 12.8 4.05 78 66 100 125 50 12 45 7.8 0.66 1.00 12.5 An array of size N is indexed from zero to N-1 INTEGER FLOAT An array can be of any type. Specific element in an array is accessed by its index. Can have more than one dimension
4. 4. 2D Array Elements Row Requires two indices Which cell is CHART [3][2]? Column [0] [1] [2] [3] [4] [5] [0] 0 97 90 268 262 130 [1] 97 0 74 337 144 128 [2] 90 74 0 354 174 201 [3] 268 337 354 0 475 269 [4] 262 144 174 475 0 238 [5] 130 128 201 269 238 0 CHART
5. 5. Arrays A particular value in an array is referenced using the array name followed by the index in brackets For example, the expression scores[2] refers to the value 45 (the 3rd value in the array) 0 1 2 3 4 5 6 50 12 45 78 66 100 125 5
6. 6. DECLARAING ARRAYS The general form of 1-d array declaration is:- type var_name[ ]; 1. Even though an array variable “scores” is declared, but there int, float, char array name is no array actually existing. 2. “score” is set to NULL, i.e. E.g.:--- int scores [ ]; an array with NO VALUE.scores int[] scores = new int[10]; NULL To link with actual, physical array of integers….
7. 7. Declaring Arrays Some examples of array declarations: double[] prices = new double[500]; boolean[] flags; flags = new boolean[20]; char[] codes = new char[1750]; 8
8. 8. ARRAY INITIALIZATION Giving values into the array created is known as INITIALIZATION. The values are delimited by braces and separated by commas Examples: int[ ] units = {147, 323, 89, 933, 540, 269, 97, 114, 298, 476}; char[ ] letterGrades = {A, B, C, D, ’F}; Note that when an initializer list is used:  the new operator is not used  no size value is specified The size of the array is determined by the number of items in the initializer list. An initializer list can only be used only in the array declaration
9. 9. ACCESSING ARRAY  A specific element in an array can be accessed by specifying its index within square brackets.  All array indexes start at ZERO. Example:- System.out.println(units[4]); mean = (units[0] + units[1])/2; 0 1 2 3 4 5 6 7 8 9int[ ] units = {147, 323, 89, 933, 540, 269, 97, 114, 298, 476};
10. 10. PROCESSING ARRAY ELEMENTS Often a for( ) loop is used to process each of the elements of the array in turn. The loop control variable, i, is used as the index to access array components EXAMPLE:- int i; score [0] for(i=0;i<=2;i++) { System.out.println(+score[i]); } 0 1 2 3 4 5 6 score 50 12 45 78 66 100 125
11. 11. int i; score [1] for(i=1;i<=2;i++) { System.out.println(+score[i]); } 0 1 2 3 4 5 6score 50 12 45 78 66 100 125
12. 12. Bounds Checking Once an array is created, it has a fixed size An index used in an array reference must specify a valid element That is, the index value must be in bounds (0 to N-1) The Java interpreter throws an ArrayIndexOutOfBoundsException if an array index is out of bounds This is called automatic bounds checking 14
13. 13. Bounds Checking For example, if the array score can hold 100 values, it can be indexed using only the numbers 0 to 99 If i has the value 100, then the following reference will cause an exception to be thrown: System.out.println (score[i]); It’s common to introduce off-by-one errors when using arrays problem for (int i=0; i <= 100; i++) score[i] = i*50;15
14. 14. ARRAY OF OBJECTS Create a class student containing data members Name, Roll_no, and Marks.WAP in JAVA to accept details of 5 students. Print names of all those students who scored greater than 85 marks student Chris,101,85 student[0] student[1] Brad, 102,75.8 student[2] student[3] Andrew, 103,75.9
15. 15. Recursion• Recursion is the process of defining something in terms of itself.• It allows a method to call itself. compute()In general, to solve a problem using recursion, youbreak it into sub problems. AREAS WHERE RECURSION CAN BE USED…………….  FACTORIAL OF A NUMBER. FIBONACCI SERIES. GCD OF TWO NUMBERS. TOWER OF HANOI. QUICK SORT. MERGE SORT.
16. 16. Recursion Example cal (5) return 5 +cal (4) return 4 +cal (3) 6 return 3 +cal (2) 3 return 2 +cal (1) TO WHOM?????? return 1 1 5 cal(5) CALLING FUNCTION
17. 17. Iteration vs. Recursion ITERATION RECURSION• Certain set of instructions • Certain set of are repeated without instructions are repeated calling function. calling function.• Uses for, while, do-while • Uses if, if-else or switch. loops.• More efficient because of • Less efficient. better execution speed.
18. 18. COMPARSION contd….• Memory utilization is • Memory utilization is less. more. • Complex to implement.• Simple to implement.• More lines of code. • Brings compactness in the program.• Terminates when loop • Terminates when base condition fails. case is satisfied.
19. 19. GCD USING RECURSIONALGORITHM:-int GCD(int a, int b) { if(b>a) return (GCD(b, a)); if(b==0) return (a); else return (GCD(b,(a%b)); }
20. 20. FIBONACCI SERIES USING RECURSION int fibo(int f1,int f2,int count) { if(count==0) then return 0; else f3=f1+f2; f1=f2; f2=f3; System.out.print(" "+f3); count--; return fibo(f1,f2,count); }
21. 21. TWO- DIMENSIONAL ARRAY  DECLARATION:- Follow the same steps as that of simple arrays. Example:- int [ ][ ]; int chart[ ][ ] = new int [3][2]; chart = new int [3][2];  INITIALIZATION:- 15 16 int chart[3][2] = { 15,16,17,18,19,20}; 17 18 19 20int chart[ ][ ] = { {15,16,17},{18,19,20} }; 15 16 17 18 19 20
22. 22. PROGRAM FOR PRACTISE The daily maximum temperature is recorded in 5 cities during 3 days. Write a program to read the table elements into 2-d array “temperature” and find the city and day corresponding to “ Highest Temperature” and “Lowest Temperature” . Write a program to create an array of objects of a class student. The class should have field members id, total marks and marks in 3 subjects viz. Physics, Chemistry & Maths. Accept information of 3 students and display them in tabular form in descending order of the total marks obtained by the student.
1. #### A particular slide catching your eye?

Clipping is a handy way to collect important slides you want to go back to later.