Printing an Array

From CompSciWiki
Revision as of 12:09, 1 April 2010 by JustinS (Talk | contribs)

Jump to: navigation, search

Back to the Program-A-Day homepage

Problem

This problem involves you printing out an array of integers using a for loop. You want to make an array of 10 integers with values you can hard code into the program. Use System.out.println() to print out the integers. Remember that you can create an array of any type with hard coded values like this:

int[] array = {1, 2, 3};

Also remember that arrays start there indexing at 0 and you can check the length of an array in Java this way:

int length = array.length;
 

SideSectionTitle

float
Taken from http://www.flickr.com/photos/daniello/565304023/

An image or By Students section

Solution

Here is the solution code to this problem:

public class Printing_Arrays
{
  public static void main (Stringp[]args)
  {
    int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    for (int i = 0; i < array.length; i++)
      System.out.println(array[i]);
  }
}

Code

SolutionCode goes here. Please DO NOT put your code in <pre> tags!

Back to the Program-A-Day homepage