Java Arrays Jussi Pohjolainen Tampere University of Applied Sciences
Types Java has two kind of types Primitive Types int, byte, short, long, double, float, boolean, char Class Types Everything else, for example String, Scanner, Arrays , Vector, JButton, JCheckBox
Problem? Implement an application that will calculate 100 students exam average. Variables needed? int studentA; int studentB; int studentC; int studentD; ...
Several Variables at Once? Array comes to the rescue! Just a list of variables Declare the array int [] array; Initialize the array and set it's size array = new array[3]; Store values into array array[0] = 2; array[1] = 3; array[2] = 7;
Array int [] myarray = new int[3]; myarray[0] = 1; myarray[1] = 12; myarray[2] = 88; System.out.println(myarray[0]); System.out.println(myarray[1]); System.out.println(myarray[2]);
myarray index length = 3 0 1 2 1 12 88
Indexes The values can be retrieved from the array using index. The first value is found from index 0 and the last from length – 1 int [] myarray = new int[LENGTH]; myarray[0] = 1; // first one myarray[LENGTH-1] = 22; // last one
Initializing the Array with {} int [] myarray = new int[3]; myarray[0] = 2; myarray[1] = 8; myarray[2] = 12; <=> int [] myarray = {2,8,12};
Common Problems int [] myarray1 = {2,8,12}; int [] myarray2 = new int[3]; // Output? System.out.println(myarray1[3]); // Output? System.out.println(myarray2[0])); // Output? System.out.println(myarray1);
Example 1 int [] myarray = {2,8,12}; int i = 0; while(i < 3) { System.out.println(myarray[i]); i++; }
Example 2 int [] myarray = {2,8,12}; int i = 0; while(i < myarray.length ) { System.out.println(myarray[i]); i++; }
Example 3 int [] myarray = {2,8,12}; for(int i = 0; i < myarray.length; i++ ) { System.out.println(myarray[i]); }
Example 3 int [] myarray = new int[3]; for(int i = 0; i < myarray.length ; i++ ) { myarray[i] = myscanner.nextInt(); } for(int i = myarray.length-1; i >= 0 ; i++ ) { System.out.println(myarray[i]); }
COMMAND LINE ARGUMENTS
Command Line Argument? public class CommandLine { public static void main( String [] args ) { } } Declaration of an array!
Command Line Argument? > java CommandLine Hello World Hello World public class CommandLine { public static void main( String [] args ) { System.out.println(args[0]); System.out.println(args[1]); } }
About Command Line Argument Command line argument is user input from the command line Argument array is initialized automatically for you (size and content)
What can go wrong? > java CommandLine 10 5 10 / 5 = 2 public class CommandLine { public static void main( String [] args ) { int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); int result = a / b; System.out.print(&quot;&quot; + a + &quot; / &quot; + b + &quot; = &quot;); System.out.println(result); } }

Java Arrays

  • 1.
    Java Arrays JussiPohjolainen Tampere University of Applied Sciences
  • 2.
    Types Java hastwo kind of types Primitive Types int, byte, short, long, double, float, boolean, char Class Types Everything else, for example String, Scanner, Arrays , Vector, JButton, JCheckBox
  • 3.
    Problem? Implement anapplication that will calculate 100 students exam average. Variables needed? int studentA; int studentB; int studentC; int studentD; ...
  • 4.
    Several Variables atOnce? Array comes to the rescue! Just a list of variables Declare the array int [] array; Initialize the array and set it's size array = new array[3]; Store values into array array[0] = 2; array[1] = 3; array[2] = 7;
  • 5.
    Array int []myarray = new int[3]; myarray[0] = 1; myarray[1] = 12; myarray[2] = 88; System.out.println(myarray[0]); System.out.println(myarray[1]); System.out.println(myarray[2]);
  • 6.
    myarray index length= 3 0 1 2 1 12 88
  • 7.
    Indexes The valuescan be retrieved from the array using index. The first value is found from index 0 and the last from length – 1 int [] myarray = new int[LENGTH]; myarray[0] = 1; // first one myarray[LENGTH-1] = 22; // last one
  • 8.
    Initializing the Arraywith {} int [] myarray = new int[3]; myarray[0] = 2; myarray[1] = 8; myarray[2] = 12; <=> int [] myarray = {2,8,12};
  • 9.
    Common Problems int[] myarray1 = {2,8,12}; int [] myarray2 = new int[3]; // Output? System.out.println(myarray1[3]); // Output? System.out.println(myarray2[0])); // Output? System.out.println(myarray1);
  • 10.
    Example 1 int[] myarray = {2,8,12}; int i = 0; while(i < 3) { System.out.println(myarray[i]); i++; }
  • 11.
    Example 2 int[] myarray = {2,8,12}; int i = 0; while(i < myarray.length ) { System.out.println(myarray[i]); i++; }
  • 12.
    Example 3 int[] myarray = {2,8,12}; for(int i = 0; i < myarray.length; i++ ) { System.out.println(myarray[i]); }
  • 13.
    Example 3 int[] myarray = new int[3]; for(int i = 0; i < myarray.length ; i++ ) { myarray[i] = myscanner.nextInt(); } for(int i = myarray.length-1; i >= 0 ; i++ ) { System.out.println(myarray[i]); }
  • 14.
  • 15.
    Command Line Argument?public class CommandLine { public static void main( String [] args ) { } } Declaration of an array!
  • 16.
    Command Line Argument?> java CommandLine Hello World Hello World public class CommandLine { public static void main( String [] args ) { System.out.println(args[0]); System.out.println(args[1]); } }
  • 17.
    About Command LineArgument Command line argument is user input from the command line Argument array is initialized automatically for you (size and content)
  • 18.
    What can gowrong? > java CommandLine 10 5 10 / 5 = 2 public class CommandLine { public static void main( String [] args ) { int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); int result = a / b; System.out.print(&quot;&quot; + a + &quot; / &quot; + b + &quot; = &quot;); System.out.println(result); } }