SlideShare a Scribd company logo
6
Most read
10
Most read
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);  } }

More Related Content

What's hot (20)

PPTX
Classes objects in java
Madishetty Prathibha
 
PPTX
String in java
Ideal Eyes Business College
 
PPTX
Arrays in Data Structure and Algorithm
KristinaBorooah
 
PPTX
Constructor ppt
Vinod Kumar
 
PPTX
Inner classes in java
PhD Research Scholar
 
PDF
Class and Objects in Java
Spotle.ai
 
PPTX
Operators in java
Then Murugeshwari
 
PDF
Java data types, variables and jvm
Madishetty Prathibha
 
PPTX
Java String
SATYAM SHRIVASTAV
 
PDF
Arrays In Python | Python Array Operations | Edureka
Edureka!
 
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
PPTX
Java Tokens
Madishetty Prathibha
 
PPT
9. Input Output in java
Nilesh Dalvi
 
PPTX
String, string builder, string buffer
SSN College of Engineering, Kalavakkam
 
PPTX
Arrays in c
CHANDAN KUMAR
 
PPTX
Data Types, Variables, and Operators
Marwa Ali Eissa
 
PPT
Core java concepts
Ram132
 
Classes objects in java
Madishetty Prathibha
 
Arrays in Data Structure and Algorithm
KristinaBorooah
 
Constructor ppt
Vinod Kumar
 
Inner classes in java
PhD Research Scholar
 
Class and Objects in Java
Spotle.ai
 
Operators in java
Then Murugeshwari
 
Java data types, variables and jvm
Madishetty Prathibha
 
Java String
SATYAM SHRIVASTAV
 
Arrays In Python | Python Array Operations | Edureka
Edureka!
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
9. Input Output in java
Nilesh Dalvi
 
String, string builder, string buffer
SSN College of Engineering, Kalavakkam
 
Arrays in c
CHANDAN KUMAR
 
Data Types, Variables, and Operators
Marwa Ali Eissa
 
Core java concepts
Ram132
 

Similar to Java Arrays (20)

PPT
17-Arrays en java presentación documento
DiegoGamboaSafla
 
PPT
ch06.ppt
AqeelAbbas94
 
PPT
ch06.ppt
ansariparveen06
 
PPT
array Details
shivas379526
 
PPT
ch06.ppt
chandrasekar529044
 
PPTX
Java arrays
Jin Castor
 
PPTX
Arrays in programming
TaseerRao
 
PPTX
Java arrays
Maneesha Caldera
 
DOCX
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
PPT
Md05 arrays
Rakesh Madugula
 
PPTX
Chapter6 (4) (1).pptx plog fix down more
mohammadalali41
 
PDF
Java chapter 6 - Arrays -syntax and use
Mukesh Tekwani
 
PPTX
ARRAYS.pptx
akila m
 
PDF
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
PPTX
Java Programming
Nanthini Kempaiyan
 
PPTX
6_Array.pptx
shafat6712
 
DOCX
Java R20 - UNIT-3.docx
Pamarthi Kumar
 
PPTX
Computer programming 2 Lesson 13
MLG College of Learning, Inc
 
PPTX
OOPs with java
AAKANKSHA JAIN
 
17-Arrays en java presentación documento
DiegoGamboaSafla
 
ch06.ppt
AqeelAbbas94
 
ch06.ppt
ansariparveen06
 
array Details
shivas379526
 
Java arrays
Jin Castor
 
Arrays in programming
TaseerRao
 
Java arrays
Maneesha Caldera
 
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
Md05 arrays
Rakesh Madugula
 
Chapter6 (4) (1).pptx plog fix down more
mohammadalali41
 
Java chapter 6 - Arrays -syntax and use
Mukesh Tekwani
 
ARRAYS.pptx
akila m
 
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
Java Programming
Nanthini Kempaiyan
 
6_Array.pptx
shafat6712
 
Java R20 - UNIT-3.docx
Pamarthi Kumar
 
Computer programming 2 Lesson 13
MLG College of Learning, Inc
 
OOPs with java
AAKANKSHA JAIN
 
Ad

More from Jussi Pohjolainen (20)

PDF
Moved to Speakerdeck
Jussi Pohjolainen
 
PDF
Java Web Services
Jussi Pohjolainen
 
PDF
Box2D and libGDX
Jussi Pohjolainen
 
PDF
libGDX: Screens, Fonts and Preferences
Jussi Pohjolainen
 
PDF
libGDX: Tiled Maps
Jussi Pohjolainen
 
PDF
libGDX: User Input and Frame by Frame Animation
Jussi Pohjolainen
 
PDF
Intro to Building Android Games using libGDX
Jussi Pohjolainen
 
PDF
Advanced JavaScript Development
Jussi Pohjolainen
 
PDF
Introduction to JavaScript
Jussi Pohjolainen
 
PDF
Introduction to AngularJS
Jussi Pohjolainen
 
PDF
libGDX: Scene2D
Jussi Pohjolainen
 
PDF
libGDX: Simple Frame Animation
Jussi Pohjolainen
 
PDF
libGDX: Simple Frame Animation
Jussi Pohjolainen
 
PDF
libGDX: User Input
Jussi Pohjolainen
 
PDF
Implementing a Simple Game using libGDX
Jussi Pohjolainen
 
PDF
Building Android games using LibGDX
Jussi Pohjolainen
 
PDF
Android Threading
Jussi Pohjolainen
 
PDF
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Jussi Pohjolainen
 
PDF
Creating Games for Asha - platform
Jussi Pohjolainen
 
PDF
Intro to Asha UI
Jussi Pohjolainen
 
Moved to Speakerdeck
Jussi Pohjolainen
 
Java Web Services
Jussi Pohjolainen
 
Box2D and libGDX
Jussi Pohjolainen
 
libGDX: Screens, Fonts and Preferences
Jussi Pohjolainen
 
libGDX: Tiled Maps
Jussi Pohjolainen
 
libGDX: User Input and Frame by Frame Animation
Jussi Pohjolainen
 
Intro to Building Android Games using libGDX
Jussi Pohjolainen
 
Advanced JavaScript Development
Jussi Pohjolainen
 
Introduction to JavaScript
Jussi Pohjolainen
 
Introduction to AngularJS
Jussi Pohjolainen
 
libGDX: Scene2D
Jussi Pohjolainen
 
libGDX: Simple Frame Animation
Jussi Pohjolainen
 
libGDX: Simple Frame Animation
Jussi Pohjolainen
 
libGDX: User Input
Jussi Pohjolainen
 
Implementing a Simple Game using libGDX
Jussi Pohjolainen
 
Building Android games using LibGDX
Jussi Pohjolainen
 
Android Threading
Jussi Pohjolainen
 
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Jussi Pohjolainen
 
Creating Games for Asha - platform
Jussi Pohjolainen
 
Intro to Asha UI
Jussi Pohjolainen
 
Ad

Recently uploaded (20)

PPTX
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
PPTX
Introduction to Indian Writing in English
Trushali Dodiya
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PDF
Council of Chalcedon Re-Examined
Smiling Lungs
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PDF
Introduction presentation of the patentbutler tool
MIPLM
 
PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PDF
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PPTX
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
Introduction to Indian Writing in English
Trushali Dodiya
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
Controller Request and Response in Odoo18
Celine George
 
Council of Chalcedon Re-Examined
Smiling Lungs
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
Introduction presentation of the patentbutler tool
MIPLM
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 

Java Arrays

  • 1. Java Arrays Jussi Pohjolainen Tampere University of Applied Sciences
  • 2. 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
  • 3. Problem? Implement an application that will calculate 100 students exam average. Variables needed? int studentA; int studentB; int studentC; int studentD; ...
  • 4. 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;
  • 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 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
  • 8. Initializing the Array with {} 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]); }
  • 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 Line Argument Command line argument is user input from the command line Argument array is initialized automatically for you (size and content)
  • 18. 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); } }