Array in Java
Array in Java
Midterm
Array in java
Introduction
What is an "array"?
Creating an array
ArrayExample.java
Array Manipulation
What is an "array"?
A graphic representation
CSStudent aStudent;
An array
int intArray[];
objects
declaration?
java.lang.Object
All array objects also have some other characteristics; i.e., each
array has an
int[] grades;
Student[] students;
individually:
Arrays are created (instantiated) with new, just like other objects.
Once an array is created, its length never changes.
Examples
int[] intArray = new int[4]; // elements initially set to 0
ArrayExample.java
/** An Example of some array manipulation
**/
int numberOfElements = 0;
if (args.length > 0) {
// Use value from command line
numberOfElements = Integer.parseInt(args[0]);
}
anExample.initializeArray(numberOfElements);
arrayLen = intArray.length;
Array Manipulation
In class example of some array manipulation
Write a Java class named Arrays.java. This class should have the following
methods.
1. public void listArgs( String [] args)
To list out the arguments in an array of Strings
2. public long product( int [] intArray )
To compute the product of the integers in the array intArray
3. public static void main( String[] args ) Should have the following code:
if (args.length == 0) {
System.out.println("usage: Arrays [integers]");
}
else {
Arrays inst = new Arrays();
inst.listArgs(args);
Refrences:
www.cs.cmu.edu