0% found this document useful (0 votes)
30 views10 pages

Oop L9

Uploaded by

RollingRage
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views10 pages

Oop L9

Uploaded by

RollingRage
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Object Oriented

Programming

BITS Pilani
CS F213
Dubai Campus
Contents
• Processing Array
• Example
• Array of objects
• Example
• Passing array to method
• Example
• Returning array from method
• Example
• Class objects for arrays
• Example
Sources:

• Copying a java array 1. Chapter 1, Cay Horstmann, Object Oriented Design & Patterns, John Wiley & Sons, 2006, 2nd Edition
2. Chapter 3, 13, Herbert Schildt, The complete Reference Java 2, 5th Edition, Tata McGraw Hill.
3. https://www.geeksforgeeks.org/arrays-in-java/
4. https://www.javatpoint.com/array-in-java
• Example 5. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
6. https://www.tutorialspoint.com/java/java_arrays.htm

BITS Pilani, Dubai Campus


Processing 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 (int i = 0; i < myList.length; i++)
{
System.out.println(myList[i] + " ");
}
// Summing all elements
double total = 0; Output:
for (int i = 0; i < myList.length; i++) 1.9
{ total += myList[i]; } 2.9
System.out.println("Total is " + total); 3.4
// Finding the largest element 3.5
double max = myList[0]; Total is 11.7
for (int i = 1; i < myList.length; i++) Max is 3.5
{ if (myList[i] > max) max = myList[i]; }
System.out.println("Max is " + max);
} Example 1: TestArray.java
}
BITS Pilani, Dubai Campus
Array of Object
• An array of objects is created just like an array of primitive
type data items.
• Syntax:
• classname[] arr_varibale = new classname[arr_size];
• Example:
• Student[] arr = new Student[7];
• Example 2: Example_2.java

BITS Pilani, Dubai Campus


Passing array to method
• Example 3: Example_3.java
class Example_3
{
public static void main(String args[])
{
int arr[] = {3, 1, 2, 5, 4};
// passing array to method m1
sum(arr);
}
public static void sum(int[] arr)
{
// getting sum of array values
int sum = 0;
for (int i = 0; i < arr.length; i++)
sum+=arr[i];
System.out.println("sum of array values : " + sum);
}
} Output:
sum of array values : 15

BITS Pilani, Dubai Campus


Returning array from method
• Example 4: Example_4.java
class Example_4
{
public static void main(String args[])
{
int arr[] = m1();
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i]+" ");
}
public static int[] m1()
{
// 1. returning array
return new int[]{1,2,3};
//2. Returning reference of array
//int[] x = new int [3];
//x[0]=1;
//x[1]=2; Output:
//x[2]=3; 1 2 3
//return(x);
}
} BITS Pilani, Dubai Campus
Class objects for arrays
• Every array has an associated Class object, shared with all other
arrays with the same component type.
• Example 5: Example_5.java
class Example_5
{
public static void main(String args[])
{
int intArray[] = new int[3];
byte byteArray[] = new byte[3];
short shortsArray[] = new short[3];
// array of Strings
String[] strArray = new String[3];
System.out.println(intArray.getClass());
System.out.println(intArray.getClass().getSuperclass());
System.out.println(byteArray.getClass());
System.out.println(shortsArray.getClass());
System.out.println(strArray.getClass());
}
}
BITS Pilani, Dubai Campus
Class objects for arrays
• Example 5 (contd..)
Output:
class [I
class java.lang.Object
class [B
class [S
class [Ljava.lang.String;

• The string “[I” is the run-time type signature for the class object “array
with component type int“.
• The only direct superclass of any array type is java.lang.Object.
• The string “[B” is the run-time type signature for the class object “array
with component type byte“.
• The string “[S” is the run-time type signature for the class object “array
with component type short“.
• The string “[L” is the run-time type signature for the class object “array
with component type of a Class”. The Class name is then followed.

BITS Pilani, Dubai Campus


Copying a java array
• Can copy an array to another by the arraycopy() method of
System class.
• Syntax:
public static void arraycopy(
Object src, int srcPos,Object dest,
int destPos, int length)
– The two Object arguments specify the array to copy from and the array to
copy to.
– The three int arguments specify the starting position in the source array, the
starting position in the destination array, and the number of array elements to
copy.
• Example 6: Example_6.java

BITS Pilani, Dubai Campus


BITS Pilani
Dubai Campus

Thank You!

You might also like