Java Arrays

6,652 views

Published on

1 Comment
3 Likes
Statistics
Notes
  • Very nice explanation, thank u so much
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
6,652
On SlideShare
0
From Embeds
0
Number of Embeds
456
Actions
Shares
0
Downloads
408
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide

Java Arrays

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

×