Lecture-3

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: c staLc void main(String args) { x = x -1; int x = 3; } while(x > 0) { if(x == 2) { if(x > 2) System.out.print(“b c”); System.out.print(“a”); } x = x – 1; public class Shuffle1 { System.out.print(“‐”); public static void main(String args) { if(x == 2) System.out.print(“b c”); if(x == 1) { ){ if(x > 2 .out.print(“a”); System.out.print(“d”); System x = x – 1; int x = 3; } } x=x–1 ; } System.o ut.print(“ –”); } } while(x > 0) { CPSC 324 ‐‐ Spring 2010 9 Java Arrays •  An array is used to store a collection of values: int theArray = {100, 10, 1000, 20, 200}; •  You access elements in an array by index theArray[0] = 100; // the first element in the array theArray[1] = 10; //the second element in the array •  You can get the length of the array using “length” System.out.print(theArray.length); // prints “5” This is very useful ... and is not available in C++! CPSC 324 ‐‐ Spring 2010 10 5 1/19/10 Java Arrays •  Using arrays in loops for(int i = 0; i < theArray.length; i++) System.out.println(theArray[i]); •  You can also use arrays in a for-each loop for(int a : theArray) System.out.println(a); This is much easier to read ... Use foreach whenever possible CPSC 324 ‐‐ Spring 2010 11 Declaring Java Arrays •  These are all equivalent ways to dec...
View Full Document

This document was uploaded on 03/18/2014 for the course CPSC 324 at Gonzaga.

Ask a homework question - tutors are online