The compiler will count the values in the initializer list and make that many

The compiler will count the values in the initializer

This preview shows page 9 - 21 out of 43 pages.

The compiler will count the values in the initializer list and make that many slots. Initializer lists are usually used only for small arrays. >> Lesson 2 9
Image of page 9

Subscribe to view the full document.

Example 3 public class Init { public static void main(String [] args) { int [] numbers = {54, 26, 99, -25, 13}; System.out.println("Contents of Array"); System.out.println("Cell 0 " + numbers[0]); System.out.println("Cell 1 " + numbers[1]); System.out.println("Cell 2 " + numbers[2]); System.out.println("Cell 3 " + numbers[3]); System.out.println("Cell 4 " + numbers[4]); } } >> Lesson 2 1 0
Image of page 10
Example 4 class ArrayDemo{ public static void main(String args[]){ int array[] = new int[7]; for (int count=0;count<7;count++){ array[count]=count+1; } for (int count=0;count<7;count++){ System.out.println("array["+count+"] = "+array[count]); } //System.out.println("Length of Array = "+array.length); // array[8] =10; } } >> Lesson 2 1 1
Image of page 11

Subscribe to view the full document.

MORE ON ARRAYS Lesson 2 >> Lesson 2 1 2
Image of page 12
Copying Value class arrayEg4 { public static void main ( String[] args ) { int[] valA = { 12, 23, 45, 56 }; int[] valB = new int[4]; valB[ 0 ] = valA[ 0 ] ; valB[ 1 ] = valA[ 1 ] ; valB[ 2 ] = valA[ 2 ] ; valB[ 3 ] = valA[ 3 ] ; } } >> Lesson 2 1 3
Image of page 13

Subscribe to view the full document.

Array as an Object (Reference) >> Lesson 2 1 4 valB = valA ;
Image of page 14
Array of Object >> Lesson 2 1 5 class Account{ int a, b; public void setData(int c,int d){ a=c; b=d; } public void showData(){ System.out.println("Value of a ="+a); System.out.println("Value of b ="+b); } } class ObjectArray{ public static void main(String args[]){ Account obj[] = new Account[2] ; obj[0] = new Account(); obj[1] = new Account(); obj[0].setData(1,2); obj[1].setData(3,4); System.out.println("For Array Element 0"); obj[0].showData(); System.out.println("For Array Element 1"); obj[1].showData(); } }
Image of page 15

Subscribe to view the full document.

PASS BY REFERENCE Lesson 3 >> Lesson 3 1 6
Image of page 16
Pass by Reference Arrays are passed to functions by reference, or as a pointer to the original. This means anything you do to the Array inside the function affects the original >> Lesson 3 1 7
Image of page 17

Subscribe to view the full document.

Example 1 class ArrayDemo { public static void passByReference( String a[] ){ a[0] = "Changed"; } public static void main(String args[]){ String []b={"Apple","Mango","Orange"}; System.out.println("Before Function Call "+b[0]); ArrayDemo.passByReference( b ); System.out.println("After Function Call "+b[0]); } } >> Lesson 3 1 8
Image of page 18
Example 2 public class SimpleArrayMethod{ public static void main(String[] args){ int a[] = new int[5]; for(int i = 0; i < 5; i++) //initialize the array: a[i] = i*5; change( a ); for(int i = 0; i < 5; i++) System.out.println(a[i] + " "); } public static void change( int[] arr ){ arr[0] = 99999; arr[1] = 66666; } } >> Lesson 3 1 9
Image of page 19

Subscribe to view the full document.

Example 3 import java.io.*; class ArrayOps { void print( int[] x ) { for ( int index=0; index < x.length; index++ )
Image of page 20
Image of page 21

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes