intro/ArrayReview

From ggc

Jump to: navigation, search

01 package intro;
02 
03 import wiki.Wiki;
04 
05 /**
06  * All about my application.
07  @author My Name Here
08  */
09 public class ArrayReview
10 {
11   public static void main(String[] args)
12   {
13     //declaring an array of integers called numbers
14     int[] numbers;
15     //intialize the array to hold 10 numbers
16     numbers=new int[10];
17     //put the number 5 in the first position of the array
18     numbers[0]=5;
19     numbers[9]=15;
20     Wiki.out.println("The array has a capacity of "+ numbers.length);
21     Wiki.out.println("Printing using standard for loop.");
22     //standard for loop
23     for(int i=0; i<numbers.length; i++)
24     {
25       Wiki.out.println("number["+i+"] is "+numbers[i]);
26     }
27     //backwards through the numbers
28     for(int i=numbers.length-1; i>=0; i--)
29 
30       Wiki.out.println("number["+i+"] is "+numbers[i]);
31 
32     //backwards through every other number
33     for(int i=numbers.length-1; i>=0; i=i-2)//i-=2 is equivalent to i=i-2
34     {
35       Wiki.out.println("number["+i+"] is "+numbers[i]);
36     }
37     Wiki.out.println("Printing using enhanced for loop.");
38     //enhanced for loop
39     int i=0;
40     for(int single: numbers)
41     {
42       Wiki.out.println("number["+i+"] is "+single);
43       i++;
44     }
45   }
46 }


Download/View intro/ArrayReview.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter