You are on page 1of 2

import java.util.

*;
public class ArrayIntroduction {
public static void main (String [ ] args)
{
// An array is a block of values of the
// same variable type.
// To create an array, add square brackets
// after the variable type, and use "new"
// to create a block of the proper type
// and size
int [ ] list = new int[5]; // 5 elements
String [ ] names = new String[3];
// size must be a positive integer
// The "length" field tells us how many
// spaces an array has in all (used and unused)
System.out.println(list.length);
// Arrays are fixed in size at creation time
// Use [ ] notation to retrieve the value at
// a specific position in the array:
// 0 - (length-1)
names[1] = "Bill";
names[2] = "David";
System.out.println(names[1]);
System.out.println(names[0]);
// (Java assigns default values to empty
// elements)
//
//
//
//
//
//
//
//
//
//
//
//

Suppose that we want to read in a series of values


from the user, but we don't know at compile time how
many values there will be. We can solve this problem
two ways:
1. Ask the user how many values there will be, and
create the array based on that response right before
reading and storing those numbers
2. Allow the user to enter the values in one list,
and figure out from that list how large to make the
array. This is more complex, but may be more user-friendly.

// Option 1:
// Create an array of a user-specified size
System.out.print("Enter the size of the array: ");
Scanner s = new Scanner(System.in);
int userSize = s.nextInt();
int [] user = new int[userSize];
// Read in the appropriate number of values
for (int i = 0; i < userSize; i++)
{

System.out.print("Enter next value: ");


user[i] = s.nextInt();
}
// Java doesn't allow us to print the contents of an array
// directly...
System.out.println(user);
// Instead, we need to write a loop that explicitly prints
// each element of the array, or we can define our own
// method (outside of main()) to print an arbitrary array.
printArray(user); // call our user-defined method
// ...
s.nextLine(); // clear buffer
// Read in an unknown number of integer values
// (version 2)
// Assume that numbers are separated by commas with no spaces
// e.g., 2,3,4
System.out.print("Enter a comma-separated list of numbers: ");
String line = s.nextLine();
// use the String method split() to break up the input by
// commas. split() returns an array that holds the pieces
// of the source String.
String [ ] divided = line.split(",");
// Create a new int array and fill it with data.
// Each element in the String array 'divided' represents
// one number.
int [] values = new int[divided.length];
for (int i = 0; i < divided.length; i++)
{
// Integer.parseInt() converts Strings to int
values[i] = Integer.parseInt(divided[i]);
}
// Print the results of the conversion
for (int i = 0; i < values.length; i++)
{
System.out.println(values[i]);
}
} // end of main()
private static void printArray (int [ ] list)
{
for (int i = 0; i < list.length; i++)
{
System.out.print(list[i] + " ");
}
System.out.println();
}
}

You might also like