More on 1-D Array - PowerPoint PPT Presentation

About This Presentation
Title:

More on 1-D Array

Description:

Assuming we have a class named Student, then we can create an array, students, ... println('i = ' i ' a[' i '] =t' a[i] 'ttb[' i '] =t' b[i] ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 10
Provided by: facultyK
Category:
Tags: array | class | more

less

Transcript and Presenter's Notes

Title: More on 1-D Array


1
More on 1-D Array
  • Overview
  • Array as a return type to methods
  • Array of Objects
  • Array Cloning
  • Preview 2-D Arrays

2
Array as return type to methods
  • We saw in the last lecture, that arrays can be
    passed as parameters to methods.
  • In addition, methods can return array as their
    result a reference to an array object.
  • import java.io.
  • class ArrayAndMethods
  • static BufferedReader stdin new
    BufferedReader( new InputStreamReader(System.in))
  • public static void main(String args) throws
    IOException
  • int size
  • System.out.print("Enter array size ")
  • size Integer.parseInt(stdin.readLine())
  • double firstArray createArray(size)
  • double secondArray createArray(size)
  • System.out.println("The dot product
    "dotProduct(firstArray, secondArray))

3
Array as return type to methods
  • static double createArray(int size) throws
    IOException
  • double array new doublesize
  • System.out.println("Enter "size" elements
    for this array ")
  • for (int i0 iltsize i)
  • arrayi Double.parseDouble(
    stdin.readLine())
  • return array
  • static double dotProduct(double first,
    double second)
  • double sum 0
  • for (int i0 iltfirst.length i)
  • sum firstisecondi
  • return sum

4
Array of Objects
  • In the examples we have seen so far, we have been
    creating arrays whose elements are primitive
    types.
  • We can equally create an array whose elements are
    objects.
  • Assuming we have a class named Student, then we
    can create an array, students, to hold 10 Student
    objects as follows
  • Student students new Student10

5
Array of Objects (contd)
  • As you can see from the figure, there is a
    fundamental difference between an array of
    primitive type and an array of object.
  • Here, the array only holds references to the
    actual objects.
  • The statement
  • Student students new Student10
  • only creates the references. To actually create
    the objects, we have to use the new operator,
    usually is a loop as follows
  • for (int i 0 i lt students.length
    i) studentsi new Student(id, grade)
  • The following is the complete Student example.
  • class Student
  • int iDNumber
  • double grade
  • public Student(int iDNumber, double grade)
  • this.iDNumber iDNumber
  • this.grade grade
  • public void print()
  • System.out.println(iDNumber"\t"grade)

6
Array of Objects (contd)
  • import java.io
  • public class ArrayOfObjects
  • static BufferedReader stdin new
    BufferedReader( new InputStreamReader(System.in))
  • public static void main(String args) throws
    IOException
  • int size
  • System.out.print("Enter number of
    students ")
  • size Integer.parseInt(stdin.readLine())
  • Student students createArray(size)
  • double average average(students)
  • System.out.println("The average is
    "average)
  • System.out.println("Students below
    average are")
  • for (int i0 iltstudents.length i)
  • if (studentsi.grade lt average)
  • studentsi.print()

7
Array of Objects (contd)
  • static Student createArray(int size) throws
    IOException
  • Student array new Studentsize
  • int id
  • double grade
  • System.out.println("Enter "size"
    students")
  • for (int i0 iltsize i)
  • System.out.print("ID Number ")
  • id Integer.parseInt(stdin.readLi
    ne())
  • System.out.print("Grade ")
  • grade Double.parseDouble(
    stdin.readLine())
  • arrayi new Student(id,
    grade)
  • return array
  • static double average(Student studentList)
  • double sum 0
  • for (int i0 iltstudentList.length
    i)
  • sum studentListi.grade
  • return sum/studentList.length

8
Array cloning
  • The following example shows how we may make a
    copy of an array. This is called clonning.
  •  
  • class ArrayCloningExample
  •   public static void main (String args)
  • int a new int5
  • int b
  • int i
  • System.out.println("Contents of a")
  • for(i 0 i lt a.length i)
  • ai i i
  • System.out.println("i "i"
    a"i"\t" ai)
  • b (int ) a.clone()
  •   System.out.println("Contents of b
    immediately after duplication from a")
  • for(i 0 i lt b.length i)
  • System.out.println("i "i" b" i "
    \t" bi)
  •   for(i 0 i lt b.length i)
  • bi 2
  • System.out.println("Contents of a and b
    after doubling elements of b")

9
Array cloning (contd)
  • for(i 0 i lt b.length i)
  • System.out.println("i " i " a"
    i " \t"ai"\t\tb" i " \t"
    bi)
  • Sample Output
  • Contents of a
  • i 0 a0 0
  • i 1 a1 1
  • i 2 a2 4
  • i 3 a3 9
  • i 4 a4 16
  • Contents of b after duplication from a
  • i 0 b0 0
  • i 1 b1 1
  • i 2 b2 4
  • i 3 b3 9
  • i 4 b4 16
  • Contents of a and b after doubling elements
    of b
  • i 0 a0 0 b0 0
Write a Comment
User Comments (0)
About PowerShow.com