SlideShare a Scribd company logo
Java
Arrays
Functions
Arrays
 Step1 : Create a reference. Cannot mention
the size.
◦ Datatype [ ] arrname;
◦ Datatype arrname[ ]; Valid – not recommended
 Step 2 : Reserve memory
◦ arrname = new datatype[SIZE];
 Step3 : Access elements
 Array index begins from 0.
 int [] a = {5 , 8 , 10}; //Valid (decl + def).
mohammed.sikander@cranessoftware.
com 2
Arrays
int [] a , b;
int c [ ] , d;
a and b; both are arrays
c is array and d is not a array
Arrays
To know the number of elements
Arr.length
int [ ] arr1 = {3 , 9 , 1, 4 , 7 };
System.out.println(arr1.length);
int [ ] arr2;
System.out.println(arr2.length);
int [ ] arr3;
arr3 = new int[10];
System.out.println(arr3.length);
Array - Accessing elements
 Method 1 :
 Find Length : arr.length
 for(int index = 0; index < arr.length ; index++)
 System.out.println(arr[index]);
 Method 2:
 for(int x : arr)
 System.out.println(x);
mohammed.sikander@cranessoftware.
com 5
int [ ] arr = {3 , 9 , 1, 4 , 7 };
for(int val : arr)
System.out.println(val);
float [ ] arr = {3 , 9 , 1, 4 , 7 };
for(float val : arr)
System.out.println(val);
int [ ] arr = {3 , 9 , 1, 4 , 7 };
int val;
for(val : arr)
System.out.println(val);
Can we use == to compare
arrays?
int [] arr1 = {7 , 5 , 2 , 8};
int [] arr2 = {7 , 5 , 2 , 8};
if(arr1 == arr2)
System.out.println("Equal");
else
System.out.println("Not Equal");
1. int [] arr1 = {7 , 5 , 2 , 8};
2. int [] arr2 ;
3. arr2 = arr1;
4. if(arr1 == arr2)
5. System.out.println("Equal");
6. else
7. System.out.println("Not
Equal");
Can we use == to compare
arrays?
Can we assign character variable to
integer?
1. char c = 'A';
2. int x = c;
Can we assign from character array to
integer array?
3. char [] carr = {'S','I','K'};
4. int [] iarr = carr;
 int [] arr;
 arr = new int[5];
 arr[0] = 12;
 arr[1] = 23;
 arr[2] = 34;

 for(int i = 0 ; i < arr.length ; i++)
 System.out.println(arr[i]);
Multi-dimensional Array
 int [] [] marks;
 int row = 3;
 int col = 4;
 marks = new int[row][col];
 for(int i = 0 ; i < row ; i++)
 for(int j = 0 ; j < col ; j++)
 marks[i][j] = i + j;
Multi-dimensional Arrays
 Method1 : Declare and initialise
 int [][] a = { {1 , 2 , 3 , 4} , {5,6,7} , {8}};
 1st row has 4 col, 3rd row has 1 col.
 System.out.println("No. of Rows = " + a.length);
 for(int x = 0 ; x < a.length ; x++)
 System.out.println("No. of Columns in " + x + " = " +
a[x].length);
 for(int r = 0 ; r < a.length ; r++)
 {
 for(int c = 0 ; c < a[r].length ; c++)
 System.out.print(a[r][c] + " ");
 } mohammed.sikander@cranessoftware.
com 12
 int [] [] marks;
 int row = 3;
 int col = 4;
 marks = new int[row][];

 for(int i = 0 ; i < row ; i++)
 for(int j = 0 ; j < col ; j++)
 marks[i][j] = i + j;
for(int i = 0 ; i < row ; i++)
marks[i] = new int[col];
Multi-dimensional Array
Multi-Dimensional Array
 int [][] mat = {{5 , 8 , 9 } , {2 , 4 } , {6}};
 mat[0][0]
 mat[0][1]
 mat[0][2]
 mat[1][0]
 mat[1][1]
 mat[1][2]
 mat[2][0]
 mat[2][1]
 mat[2][2]
 mat[0][0]
 mat[0][1]
 mat[0][2]
 mat[1][0]
 mat[1][1]
 mat[1][2]
 mat[2][0]
 mat[2][1]
 mat[2][2]
Copying Arrays
int [] arr1 = {5 ,8 , 10, 15, 20};
int [] arr2;
arr2 = arr1;
for(int x: arr2)
System.out.println(x);
arr1[0] = 2; arr1[1] = 4;
for(int x: arr2)
System.out.println(x);
 System.arraycopy(src Array, src pos,
destination Array, dest pos, length);
 int [] arr1 = {5 ,8 , 10, 15, 20};
 int [] arr2 = new int[5];

 System.arraycopy(arr1, 0, arr2, 0, 5);
int [] arr1 = {5 ,8 , 10, 15, 20};
int [] arr2 = Arrays.copyOf(arr1, 5);
for(int x: arr2)
System.out.println(x);
Sorting of Array
 Arrays.sort(Type [] arr);
 Arrays.sort(Type [], int fromIndex, int toIndex);
 int arr[] = {5,2,9,1,2,8};
 Arrays.sort(arr); OR
 Arrays.sort(arr,0,6);
 Output : 1 2 2 5 8 9
 int arr[] = {5,2,9,1,2,8};
 Arrays.sort(arr,1,5);
 Output : 5 1 2 2 9 8
Anonymous Arrays
public static void printArray(int [] x)
{
for(int i : x)
System.out.println(x);
}
public static void main(String [] args)
{
int [] marks = {3,6,4,7};
1. printArray(marks);
2. printArray(1 ,3 ,5 ,7);
3. printArray([]{1 ,3 ,4 ,5});
4. printArray(new int []{1 ,3 ,4 ,5});
}
Identify the valid calls to
printArray?
FUNCTIONS
Argument Passing
public static void function(int x)
{
x = 20;
System.out.println("Function " + x);
}
public static void main(String [] args)
{
int x = 5;
function(x);
System.out.println("Main " + x);
}
Passing Array to Function
public class ArgumentPassingArray {
public static void function(int [] arr)
{
arr[0] = 20;
}
public static void main(String [] args)
{
int [] arr = {5,8,10};
function(arr);
System.out.println("Main " + arr[0]);
}
}
String str;
str = new String("SIKANDER");
System.out.println(str.hashCode());
str = str.toLowerCase();
System.out.println(str.hashCode());
Argument Passing to Function
class Rectangle {
int length ;
int breadth;
Rectangle(int l , int b) {
length = l ;
breadth = b;
}
}
public class FunctionDemo {
public static void function(Rectangle x)
{
x.length = 20;
System.out.println("Function " + x.length + " " + x.breadth);
}
public static void main(String [] args)
{
Rectangle obj = new Rectangle(4 ,5 );
function(obj);
System.out.println("Main " + obj.length + " " + obj.breadth);
}
}
class Rectangle {
int length ;
int breadth;
Rectangle(int l , int b) {
length = l ;
breadth = b;
}
}
public class FunctionDemo {
public static void function(Rectangle x)
{
x = new Rectangle( 7 , 9);
System.out.println("Function " + x.length + " " + x.breadth);
}
public static void main(String [] args)
{
Rectangle obj = new Rectangle(4 ,5 );
function(obj);
System.out.println("Main " + obj.length + " " + obj.breadth);
}
}

More Related Content

PPTX
Arrays in java
bhavesh prakash
 
PPT
Java Arrays
Jussi Pohjolainen
 
PPT
Java: GUI
Tareq Hasan
 
PPTX
Two-dimensional array in java
Talha mahmood
 
PPTX
Arrays in Java
Abhilash Nair
 
PPTX
Introduction to Java Strings, By Kavita Ganesan
Kavita Ganesan
 
PPTX
Polymorphism in java
Elizabeth alexander
 
PDF
Arrays in Java | Edureka
Edureka!
 
Arrays in java
bhavesh prakash
 
Java Arrays
Jussi Pohjolainen
 
Java: GUI
Tareq Hasan
 
Two-dimensional array in java
Talha mahmood
 
Arrays in Java
Abhilash Nair
 
Introduction to Java Strings, By Kavita Ganesan
Kavita Ganesan
 
Polymorphism in java
Elizabeth alexander
 
Arrays in Java | Edureka
Edureka!
 

What's hot (20)

PPTX
Polymorphism presentation in java
Ahsan Raja
 
PPT
Python Dictionaries and Sets
Nicole Ryan
 
ODP
Java Collections
parag
 
PPT
java programming - applets
HarshithaAllu
 
PPTX
Regular Expression
valuebound
 
PPTX
First and follow set
Dawood Faheem Abbasi
 
PPT
Virtual Reality Modeling Language
Swati Chauhan
 
PPTX
Regular Expression (Regex) Fundamentals
Mesut Günes
 
PPTX
sum of subset problem using Backtracking
Abhishek Singh
 
PPTX
Array in c#
Prem Kumar Badri
 
PDF
Java modifiers
Soba Arjun
 
PDF
Python-02| Input, Output & Import
Mohd Sajjad
 
PPT
Java interfaces
Raja Sekhar
 
PPTX
HOME INTRUSION DETECTION.pptx
PavaniMachineni
 
PPT
Wrapper class (130240116056)
Akshay soni
 
PPTX
Java - Generic programming
Riccardo Cardin
 
PPTX
Implementation of trees
Mubashar Mehmood
 
PPTX
Super keyword in java
Hitesh Kumar
 
PDF
Lecture 2 role of algorithms in computing
jayavignesh86
 
PPTX
Regular expressions
Ratnakar Mikkili
 
Polymorphism presentation in java
Ahsan Raja
 
Python Dictionaries and Sets
Nicole Ryan
 
Java Collections
parag
 
java programming - applets
HarshithaAllu
 
Regular Expression
valuebound
 
First and follow set
Dawood Faheem Abbasi
 
Virtual Reality Modeling Language
Swati Chauhan
 
Regular Expression (Regex) Fundamentals
Mesut Günes
 
sum of subset problem using Backtracking
Abhishek Singh
 
Array in c#
Prem Kumar Badri
 
Java modifiers
Soba Arjun
 
Python-02| Input, Output & Import
Mohd Sajjad
 
Java interfaces
Raja Sekhar
 
HOME INTRUSION DETECTION.pptx
PavaniMachineni
 
Wrapper class (130240116056)
Akshay soni
 
Java - Generic programming
Riccardo Cardin
 
Implementation of trees
Mubashar Mehmood
 
Super keyword in java
Hitesh Kumar
 
Lecture 2 role of algorithms in computing
jayavignesh86
 
Regular expressions
Ratnakar Mikkili
 
Ad

Viewers also liked (6)

PPT
Array in Java
Shehrevar Davierwala
 
PDF
Java Variable Types
Shahid Rasheed
 
PPTX
Arrays in java
Arzath Areeff
 
PPT
Java: Introduction to Arrays
Tareq Hasan
 
PPT
3 java - variable type
vinay arora
 
PPS
Introduction to class in java
kamal kotecha
 
Array in Java
Shehrevar Davierwala
 
Java Variable Types
Shahid Rasheed
 
Arrays in java
Arzath Areeff
 
Java: Introduction to Arrays
Tareq Hasan
 
3 java - variable type
vinay arora
 
Introduction to class in java
kamal kotecha
 
Ad

Similar to Java arrays (20)

PPT
SP-First-Lecture.ppt
FareedIhsas
 
PDF
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
eyewatchsystems
 
PPT
Multi dimensional arrays
Aseelhalees
 
PPTX
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...
MaruMengesha
 
PPT
arrays
teach4uin
 
PDF
11 1. multi-dimensional array eng
웅식 전
 
PPTX
C (PPS)Programming for problem solving.pptx
rohinitalekar1
 
PPTX
6_Array.pptx
shafat6712
 
DOCX
QA Auotmation Java programs,theory
archana singh
 
PPTX
07. Arrays
Intro C# Book
 
DOC
Experiment 06 psiplclannguage expermient.doc
anuharshpawar22
 
PPTX
Unit 3
GOWSIKRAJAP
 
PPT
Arrays in c programing. practicals and .ppt
Carlos701746
 
PPT
Arrays 06.ppt
ahtishamtariq511
 
PPT
Array i imp
Vivek Kumar
 
PDF
Computer java programs
ADITYA BHARTI
 
PPT
2DArrays.ppt
Nooryaseen9
 
PPT
Chapter 6 arrays part-1
Synapseindiappsdevelopment
 
PDF
Lecture 6
Debasish Pratihari
 
PPT
array2d.ppt
DeveshDewangan5
 
SP-First-Lecture.ppt
FareedIhsas
 
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
eyewatchsystems
 
Multi dimensional arrays
Aseelhalees
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...
MaruMengesha
 
arrays
teach4uin
 
11 1. multi-dimensional array eng
웅식 전
 
C (PPS)Programming for problem solving.pptx
rohinitalekar1
 
6_Array.pptx
shafat6712
 
QA Auotmation Java programs,theory
archana singh
 
07. Arrays
Intro C# Book
 
Experiment 06 psiplclannguage expermient.doc
anuharshpawar22
 
Unit 3
GOWSIKRAJAP
 
Arrays in c programing. practicals and .ppt
Carlos701746
 
Arrays 06.ppt
ahtishamtariq511
 
Array i imp
Vivek Kumar
 
Computer java programs
ADITYA BHARTI
 
2DArrays.ppt
Nooryaseen9
 
Chapter 6 arrays part-1
Synapseindiappsdevelopment
 
array2d.ppt
DeveshDewangan5
 

More from Mohammed Sikander (20)

PPTX
Strings in C - covers string functions
Mohammed Sikander
 
PDF
Smart Pointers, Modern Memory Management Techniques
Mohammed Sikander
 
PDF
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
PDF
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Mohammed Sikander
 
PDF
Operator Overloading in C++
Mohammed Sikander
 
PDF
Python_Regular Expression
Mohammed Sikander
 
PPTX
Modern_CPP-Range-Based For Loop.pptx
Mohammed Sikander
 
PDF
Modern_cpp_auto.pdf
Mohammed Sikander
 
PPTX
Python Functions
Mohammed Sikander
 
PPTX
Python dictionary
Mohammed Sikander
 
PDF
Python exception handling
Mohammed Sikander
 
PDF
Python tuple
Mohammed Sikander
 
PDF
Python strings
Mohammed Sikander
 
PDF
Python set
Mohammed Sikander
 
PDF
Python list
Mohammed Sikander
 
PDF
Python Flow Control
Mohammed Sikander
 
PDF
Introduction to Python
Mohammed Sikander
 
PPTX
Pointer basics
Mohammed Sikander
 
PPTX
Signal
Mohammed Sikander
 
Strings in C - covers string functions
Mohammed Sikander
 
Smart Pointers, Modern Memory Management Techniques
Mohammed Sikander
 
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Mohammed Sikander
 
Operator Overloading in C++
Mohammed Sikander
 
Python_Regular Expression
Mohammed Sikander
 
Modern_CPP-Range-Based For Loop.pptx
Mohammed Sikander
 
Modern_cpp_auto.pdf
Mohammed Sikander
 
Python Functions
Mohammed Sikander
 
Python dictionary
Mohammed Sikander
 
Python exception handling
Mohammed Sikander
 
Python tuple
Mohammed Sikander
 
Python strings
Mohammed Sikander
 
Python set
Mohammed Sikander
 
Python list
Mohammed Sikander
 
Python Flow Control
Mohammed Sikander
 
Introduction to Python
Mohammed Sikander
 
Pointer basics
Mohammed Sikander
 

Recently uploaded (20)

PDF
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Q-Advise
 
PDF
The Future of Smart Factories Why Embedded Analytics Leads the Way
Varsha Nayak
 
PPTX
Benefits of DCCM for Genesys Contact Center
pointel ivr
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
DOCX
The Future of Smart Factories Why Embedded Analytics Leads the Way
Varsha Nayak
 
PDF
Why Should Businesses Extract Cuisine Types Data from Multiple U.S. Food Apps...
devilbrown689
 
PDF
Become an Agentblazer Champion Challenge Kickoff
Dele Amefo
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
What to Capture When It Breaks: 16 Artifacts That Reveal Root Causes
Tier1 app
 
PPTX
Save Business Costs with CRM Software for Insurance Agents
Insurance Tech Services
 
PDF
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
PDF
Micromaid: A simple Mermaid-like chart generator for Pharo
ESUG
 
PPTX
10 Hidden App Development Costs That Can Sink Your Startup.pptx
Lunar Web Solution
 
PDF
Community & News Update Q2 Meet Up 2025
VictoriaMetrics
 
PPT
Order to Cash Lifecycle Overview R12 .ppt
nbvreddy229
 
PDF
Emergency Mustering solutions – A Brief overview
Personnel Tracking
 
PDF
The Role of Automation and AI in EHS Management for Data Centers.pdf
TECH EHS Solution
 
PPTX
Hire Expert Blazor Developers | Scalable Solutions by OnestopDA
OnestopDA
 
PDF
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Q-Advise
 
The Future of Smart Factories Why Embedded Analytics Leads the Way
Varsha Nayak
 
Benefits of DCCM for Genesys Contact Center
pointel ivr
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
The Future of Smart Factories Why Embedded Analytics Leads the Way
Varsha Nayak
 
Why Should Businesses Extract Cuisine Types Data from Multiple U.S. Food Apps...
devilbrown689
 
Become an Agentblazer Champion Challenge Kickoff
Dele Amefo
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
What to Capture When It Breaks: 16 Artifacts That Reveal Root Causes
Tier1 app
 
Save Business Costs with CRM Software for Insurance Agents
Insurance Tech Services
 
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
Micromaid: A simple Mermaid-like chart generator for Pharo
ESUG
 
10 Hidden App Development Costs That Can Sink Your Startup.pptx
Lunar Web Solution
 
Community & News Update Q2 Meet Up 2025
VictoriaMetrics
 
Order to Cash Lifecycle Overview R12 .ppt
nbvreddy229
 
Emergency Mustering solutions – A Brief overview
Personnel Tracking
 
The Role of Automation and AI in EHS Management for Data Centers.pdf
TECH EHS Solution
 
Hire Expert Blazor Developers | Scalable Solutions by OnestopDA
OnestopDA
 
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 

Java arrays

  • 2. Arrays  Step1 : Create a reference. Cannot mention the size. ◦ Datatype [ ] arrname; ◦ Datatype arrname[ ]; Valid – not recommended  Step 2 : Reserve memory ◦ arrname = new datatype[SIZE];  Step3 : Access elements  Array index begins from 0.  int [] a = {5 , 8 , 10}; //Valid (decl + def). mohammed.sikander@cranessoftware. com 2
  • 3. Arrays int [] a , b; int c [ ] , d; a and b; both are arrays c is array and d is not a array
  • 4. Arrays To know the number of elements Arr.length int [ ] arr1 = {3 , 9 , 1, 4 , 7 }; System.out.println(arr1.length); int [ ] arr2; System.out.println(arr2.length); int [ ] arr3; arr3 = new int[10]; System.out.println(arr3.length);
  • 5. Array - Accessing elements  Method 1 :  Find Length : arr.length  for(int index = 0; index < arr.length ; index++)  System.out.println(arr[index]);  Method 2:  for(int x : arr)  System.out.println(x); mohammed.sikander@cranessoftware. com 5
  • 6. int [ ] arr = {3 , 9 , 1, 4 , 7 }; for(int val : arr) System.out.println(val); float [ ] arr = {3 , 9 , 1, 4 , 7 }; for(float val : arr) System.out.println(val); int [ ] arr = {3 , 9 , 1, 4 , 7 }; int val; for(val : arr) System.out.println(val);
  • 7. Can we use == to compare arrays? int [] arr1 = {7 , 5 , 2 , 8}; int [] arr2 = {7 , 5 , 2 , 8}; if(arr1 == arr2) System.out.println("Equal"); else System.out.println("Not Equal");
  • 8. 1. int [] arr1 = {7 , 5 , 2 , 8}; 2. int [] arr2 ; 3. arr2 = arr1; 4. if(arr1 == arr2) 5. System.out.println("Equal"); 6. else 7. System.out.println("Not Equal"); Can we use == to compare arrays?
  • 9. Can we assign character variable to integer? 1. char c = 'A'; 2. int x = c; Can we assign from character array to integer array? 3. char [] carr = {'S','I','K'}; 4. int [] iarr = carr;
  • 10.  int [] arr;  arr = new int[5];  arr[0] = 12;  arr[1] = 23;  arr[2] = 34;   for(int i = 0 ; i < arr.length ; i++)  System.out.println(arr[i]);
  • 11. Multi-dimensional Array  int [] [] marks;  int row = 3;  int col = 4;  marks = new int[row][col];  for(int i = 0 ; i < row ; i++)  for(int j = 0 ; j < col ; j++)  marks[i][j] = i + j;
  • 12. Multi-dimensional Arrays  Method1 : Declare and initialise  int [][] a = { {1 , 2 , 3 , 4} , {5,6,7} , {8}};  1st row has 4 col, 3rd row has 1 col.  System.out.println("No. of Rows = " + a.length);  for(int x = 0 ; x < a.length ; x++)  System.out.println("No. of Columns in " + x + " = " + a[x].length);  for(int r = 0 ; r < a.length ; r++)  {  for(int c = 0 ; c < a[r].length ; c++)  System.out.print(a[r][c] + " ");  } mohammed.sikander@cranessoftware. com 12
  • 13.  int [] [] marks;  int row = 3;  int col = 4;  marks = new int[row][];   for(int i = 0 ; i < row ; i++)  for(int j = 0 ; j < col ; j++)  marks[i][j] = i + j; for(int i = 0 ; i < row ; i++) marks[i] = new int[col]; Multi-dimensional Array
  • 14. Multi-Dimensional Array  int [][] mat = {{5 , 8 , 9 } , {2 , 4 } , {6}};  mat[0][0]  mat[0][1]  mat[0][2]  mat[1][0]  mat[1][1]  mat[1][2]  mat[2][0]  mat[2][1]  mat[2][2]  mat[0][0]  mat[0][1]  mat[0][2]  mat[1][0]  mat[1][1]  mat[1][2]  mat[2][0]  mat[2][1]  mat[2][2]
  • 15. Copying Arrays int [] arr1 = {5 ,8 , 10, 15, 20}; int [] arr2; arr2 = arr1; for(int x: arr2) System.out.println(x); arr1[0] = 2; arr1[1] = 4; for(int x: arr2) System.out.println(x);
  • 16.  System.arraycopy(src Array, src pos, destination Array, dest pos, length);  int [] arr1 = {5 ,8 , 10, 15, 20};  int [] arr2 = new int[5];   System.arraycopy(arr1, 0, arr2, 0, 5);
  • 17. int [] arr1 = {5 ,8 , 10, 15, 20}; int [] arr2 = Arrays.copyOf(arr1, 5); for(int x: arr2) System.out.println(x);
  • 18. Sorting of Array  Arrays.sort(Type [] arr);  Arrays.sort(Type [], int fromIndex, int toIndex);  int arr[] = {5,2,9,1,2,8};  Arrays.sort(arr); OR  Arrays.sort(arr,0,6);  Output : 1 2 2 5 8 9  int arr[] = {5,2,9,1,2,8};  Arrays.sort(arr,1,5);  Output : 5 1 2 2 9 8
  • 19. Anonymous Arrays public static void printArray(int [] x) { for(int i : x) System.out.println(x); } public static void main(String [] args) { int [] marks = {3,6,4,7}; 1. printArray(marks); 2. printArray(1 ,3 ,5 ,7); 3. printArray([]{1 ,3 ,4 ,5}); 4. printArray(new int []{1 ,3 ,4 ,5}); } Identify the valid calls to printArray?
  • 21. Argument Passing public static void function(int x) { x = 20; System.out.println("Function " + x); } public static void main(String [] args) { int x = 5; function(x); System.out.println("Main " + x); }
  • 22. Passing Array to Function public class ArgumentPassingArray { public static void function(int [] arr) { arr[0] = 20; } public static void main(String [] args) { int [] arr = {5,8,10}; function(arr); System.out.println("Main " + arr[0]); } }
  • 23. String str; str = new String("SIKANDER"); System.out.println(str.hashCode()); str = str.toLowerCase(); System.out.println(str.hashCode());
  • 24. Argument Passing to Function class Rectangle { int length ; int breadth; Rectangle(int l , int b) { length = l ; breadth = b; } } public class FunctionDemo { public static void function(Rectangle x) { x.length = 20; System.out.println("Function " + x.length + " " + x.breadth); } public static void main(String [] args) { Rectangle obj = new Rectangle(4 ,5 ); function(obj); System.out.println("Main " + obj.length + " " + obj.breadth); } }
  • 25. class Rectangle { int length ; int breadth; Rectangle(int l , int b) { length = l ; breadth = b; } } public class FunctionDemo { public static void function(Rectangle x) { x = new Rectangle( 7 , 9); System.out.println("Function " + x.length + " " + x.breadth); } public static void main(String [] args) { Rectangle obj = new Rectangle(4 ,5 ); function(obj); System.out.println("Main " + obj.length + " " + obj.breadth); } }

Editor's Notes

  • #4: Error: b is an array
  • #5: Length can be access only if array has been defined (using new / initialised eles).
  • #6: Length can be access only if array has been defined (using new / initialised eles).
  • #8: No Compilation Error Output : Not Equal , as they are pointing to different arrays.
  • #9: Line 3 : Valid Output : Equal.
  • #10: Line 2 is valid Line 4 is not Valid
  • #11: 12 23 34 0 0
  • #12: Draw the diagram: Assume marks as pointer to pointer New int[row][col]; will first create an array of pointers [row] and then Allocates memory for each row (col no. of elements). We can place the elements
  • #13: No. of Rows = 4 No. of Columns in 0 = 4 No. of Columns in 1 = 3 No. of Columns in 2 = 1 1 2 3 4 5 6 7 8
  • #14: Error :
  • #16: public class ArrayCopy { public static void main(String [] args) { int [] arr1 = {5 ,8 , 10, 15, 20}; int [] arr2; arr2 = arr1; //Copies only reference. for(int x: arr2) System.out.println(x); arr1[0] = 2; arr1[1] = 4; for(int x: arr2) System.out.println(x); } }
  • #17: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html public class ArrayCopyMethod { public static void main(String [] args) { int [] arr1 = {5 ,8 , 10, 15, 20}; int [] arr2 = new int[5]; System.arraycopy(arr1, 0, arr2, 0, 5); for(int x: arr2) System.out.println(x); arr1[0] = 2; arr1[1] = 4; for(int x: arr2) System.out.println(x); } }
  • #18: import java.util.*; public class ArrayCopyMethod { public static void main(String [] args) { int [] arr1 = {5 ,8 , 10, 15, 20}; //int [] arr2 = new int[5]; //System.arraycopy(arr1, 0, arr2, 0, 5); int [] arr2 = Arrays.copyOf(arr1, 5); for(int x: arr2) System.out.println(x); // arr1[0] = 2; arr1[1] = 4; // // for(int x: arr2) // System.out.println(x); } }
  • #20: Line 2 and 3 will not work
  • #22: Its pass by Value
  • #23: Pass by reference
  • #25: Reference types are always passed by reference.
  • #26: X refers to new object.