SlideShare uma empresa Scribd logo
1 de 19
Java Arrays
What is an Array?
• So far, you have been working with variables
that hold only one value. The integer variables
you have set up have held only one
number, and the string variables just one long
string of text.
What is an Array?
• An array is a way to hold more than one value
at a time. It's like a list of items. Think of an
array as the columns in a spreadsheet. You can
have a spreadsheet with only one column, or
lots of columns.
• The data held in a single-list array might look
like this:
• Like a spreadsheet, arrays have a position
number for each row. The positions in an array
start at 0 and go up sequentially. Each position
in the array can then hold a value. In the
image above array position 0 is holding a value
of 10, array position 1 is holding a value of 14,
position 2 has a value of 36, and so on.
• To set up an array of number like that in the
image above, you have to tell Java what kind
of data is going in to your array (integers,
strings, boolean values, etc). You then need to
say how many positions the array has. You set
them up like this:
int[ ] aryNums;
• The only difference between setting up a
normal integer variable and an array is a pair
of square brackets after the data type. The
square brackets are enough to tell Java that
you want to set up an array. The name of the
array above is aryNums. Just like normal
variables, you can call them almost anything
you like (with the same exceptions we
mentioned earlier).
• But this just tells Java that you want to set up
an integer array. It doesn't say how many
positions the array should hold. To do
that, you have to set up a new array object:
aryNums = new int[6];
You start with your array name, followed by the
equals sign. After the equals sign, you need the
Java keyword new, and then your data type
again. After the data type come a pair of square
brackets. In between the square brackets you
need the size of the array. The size is how many
positions the array should hold.
• If you prefer, you can put all that on one line:
int[ ] aryNums = new int[6];
• So we are telling Java to set up an array with 6
positions in it. After this line is executed, Java
will assign default values for the array.
Because we've set up an integer array, the
default values for all 6 positions will be zero (
0 ).
• To assign values to the various positions in an
array, you do it in the normal way:
aryNums[0] = 10;
• Here, a value of 10 is being assigned to
position 0 in the array called aryNums. Again,
the square brackets are used to refer to each
position. If you want to assign a value of 14 to
array position 1, the code would be this:
aryNums[1] = 14;
• And to assign a value of 36 to array position 2,
it's this:
aryNums[2] = 36;
• Don't forget, because arrays start at 0, the
third position in an array has the index
number 2.
• If you know what values are going to be in the
array, you can set them up like this instead:
int[ ] aryNums = { 1, 2, 3, 4 };
This method of setting up an array uses curly
brackets after the equals sign.
• In between the curly brackets, you type out the
values that the array will hold. The first value will
then be position 0, the second value position 1,
and so on. Note that you still need the square
brackets after int, but not the new keyword, or
the repetition of the data type and square
brackets. But this is just for data types of int
values, string, and char values. Otherwise, you
need the new keyword. So you can do this:
String[ ] aryStrings = {"Autumn", "Spring",
"Summer", "Winter" };
• But not this:
boolean[ ] aryBools =
{false, true, false, true};
• To set up a boolean array you still need the
new keyword:
boolean[ ] aryBools = new boolean[ ]
{false, true, false, true};
• To get at the values held in your array, you
type the name of the array followed by an
array position in square brackets. Like this:
System.out.println( aryNums[2] );
• The above code will print out whatever value
is held at array position 2 in the array called
aryNums. But let's get some coding practice.
• Start a new project and call it anything you
like. Don't forget to change the name of the
Class to something relevant.
• Type the following code into your new Main method:

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
An Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysAn Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: Arrays
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Java basics and java variables
Java basics and java variablesJava basics and java variables
Java basics and java variables
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS Concept
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Java constructors
Java constructorsJava constructors
Java constructors
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
StringTokenizer in java
StringTokenizer in javaStringTokenizer in java
StringTokenizer in java
 
Autoboxing And Unboxing In Java
Autoboxing And Unboxing In JavaAutoboxing And Unboxing In Java
Autoboxing And Unboxing In Java
 
Java Stack Data Structure.pptx
Java Stack Data Structure.pptxJava Stack Data Structure.pptx
Java Stack Data Structure.pptx
 
Methods in java
Methods in javaMethods in java
Methods in java
 
Java Strings
Java StringsJava Strings
Java Strings
 
Inheritance
InheritanceInheritance
Inheritance
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 

Destaque (6)

Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Array in Java
Array in JavaArray in Java
Array in Java
 
intorduction to Arrays in java
intorduction to Arrays in javaintorduction to Arrays in java
intorduction to Arrays in java
 
Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)
 
Arrays
ArraysArrays
Arrays
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java language
 

Semelhante a Java arrays

Programming fundamentals week 12.pptx
Programming fundamentals week 12.pptxProgramming fundamentals week 12.pptx
Programming fundamentals week 12.pptx
dfsdg3
 
5 ARRAYS AND STRINGSjiuojhiooioiiouioi.pptx
5 ARRAYS AND STRINGSjiuojhiooioiiouioi.pptx5 ARRAYS AND STRINGSjiuojhiooioiiouioi.pptx
5 ARRAYS AND STRINGSjiuojhiooioiiouioi.pptx
teddiyfentaw
 

Semelhante a Java arrays (20)

Programming fundamentals week 12.pptx
Programming fundamentals week 12.pptxProgramming fundamentals week 12.pptx
Programming fundamentals week 12.pptx
 
Chapter-Five.pptx
Chapter-Five.pptxChapter-Five.pptx
Chapter-Five.pptx
 
Basics of array.pptx
Basics of array.pptxBasics of array.pptx
Basics of array.pptx
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
 
Arrays in c++
Arrays in c++Arrays in c++
Arrays in c++
 
Comp102 lec 8
Comp102   lec 8Comp102   lec 8
Comp102 lec 8
 
Arrays
ArraysArrays
Arrays
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Java arrays (1)
Java arrays (1)Java arrays (1)
Java arrays (1)
 
Array
ArrayArray
Array
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
Learn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & ArraysLearn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & Arrays
 
Arrays in java (signle dimensional array)
Arrays in java (signle dimensional array)Arrays in java (signle dimensional array)
Arrays in java (signle dimensional array)
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
 
Array
ArrayArray
Array
 
C
CC
C
 
PHP array 1
PHP array 1PHP array 1
PHP array 1
 
5 ARRAYS AND STRINGSjiuojhiooioiiouioi.pptx
5 ARRAYS AND STRINGSjiuojhiooioiiouioi.pptx5 ARRAYS AND STRINGSjiuojhiooioiiouioi.pptx
5 ARRAYS AND STRINGSjiuojhiooioiiouioi.pptx
 
Core java day2
Core java day2Core java day2
Core java day2
 
Unit ii data structure-converted
Unit  ii data structure-convertedUnit  ii data structure-converted
Unit ii data structure-converted
 

Mais de Jin Castor

Introduction to search engine optimization
Introduction to search engine optimizationIntroduction to search engine optimization
Introduction to search engine optimization
Jin Castor
 
Web services protocols
Web services protocolsWeb services protocols
Web services protocols
Jin Castor
 
Web application security
Web application securityWeb application security
Web application security
Jin Castor
 
Introduction to xampp
Introduction to xamppIntroduction to xampp
Introduction to xampp
Jin Castor
 
Drupal introduction
Drupal introductionDrupal introduction
Drupal introduction
Jin Castor
 
Control statements in Java
Control statements  in JavaControl statements  in Java
Control statements in Java
Jin Castor
 
Switch statements in Java
Switch statements  in JavaSwitch statements  in Java
Switch statements in Java
Jin Castor
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
Jin Castor
 

Mais de Jin Castor (17)

Information security
 Information security Information security
Information security
 
Introduction to E-commerce
Introduction to E-commerceIntroduction to E-commerce
Introduction to E-commerce
 
Introduction to Infographics Designing
Introduction to Infographics DesigningIntroduction to Infographics Designing
Introduction to Infographics Designing
 
Creative designing using Adobe Products
Creative designing using Adobe ProductsCreative designing using Adobe Products
Creative designing using Adobe Products
 
Introduction to Adobe Illustrator
Introduction to Adobe IllustratorIntroduction to Adobe Illustrator
Introduction to Adobe Illustrator
 
SEO Advanced and scalable link building
SEO  Advanced and scalable link building SEO  Advanced and scalable link building
SEO Advanced and scalable link building
 
Introduction to Web Designing
Introduction to Web DesigningIntroduction to Web Designing
Introduction to Web Designing
 
Introduction to search engine optimization
Introduction to search engine optimizationIntroduction to search engine optimization
Introduction to search engine optimization
 
Web services protocols
Web services protocolsWeb services protocols
Web services protocols
 
Web application security
Web application securityWeb application security
Web application security
 
Introduction to xampp
Introduction to xamppIntroduction to xampp
Introduction to xampp
 
Drupal introduction
Drupal introductionDrupal introduction
Drupal introduction
 
Web security
Web securityWeb security
Web security
 
Control statements in Java
Control statements  in JavaControl statements  in Java
Control statements in Java
 
Switch statements in Java
Switch statements  in JavaSwitch statements  in Java
Switch statements in Java
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
Java input
Java inputJava input
Java input
 

Último

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Último (20)

The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 

Java arrays

  • 2. What is an Array? • So far, you have been working with variables that hold only one value. The integer variables you have set up have held only one number, and the string variables just one long string of text.
  • 3. What is an Array? • An array is a way to hold more than one value at a time. It's like a list of items. Think of an array as the columns in a spreadsheet. You can have a spreadsheet with only one column, or lots of columns.
  • 4. • The data held in a single-list array might look like this:
  • 5. • Like a spreadsheet, arrays have a position number for each row. The positions in an array start at 0 and go up sequentially. Each position in the array can then hold a value. In the image above array position 0 is holding a value of 10, array position 1 is holding a value of 14, position 2 has a value of 36, and so on.
  • 6. • To set up an array of number like that in the image above, you have to tell Java what kind of data is going in to your array (integers, strings, boolean values, etc). You then need to say how many positions the array has. You set them up like this: int[ ] aryNums;
  • 7. • The only difference between setting up a normal integer variable and an array is a pair of square brackets after the data type. The square brackets are enough to tell Java that you want to set up an array. The name of the array above is aryNums. Just like normal variables, you can call them almost anything you like (with the same exceptions we mentioned earlier).
  • 8. • But this just tells Java that you want to set up an integer array. It doesn't say how many positions the array should hold. To do that, you have to set up a new array object: aryNums = new int[6];
  • 9. You start with your array name, followed by the equals sign. After the equals sign, you need the Java keyword new, and then your data type again. After the data type come a pair of square brackets. In between the square brackets you need the size of the array. The size is how many positions the array should hold.
  • 10. • If you prefer, you can put all that on one line: int[ ] aryNums = new int[6]; • So we are telling Java to set up an array with 6 positions in it. After this line is executed, Java will assign default values for the array. Because we've set up an integer array, the default values for all 6 positions will be zero ( 0 ).
  • 11. • To assign values to the various positions in an array, you do it in the normal way: aryNums[0] = 10;
  • 12. • Here, a value of 10 is being assigned to position 0 in the array called aryNums. Again, the square brackets are used to refer to each position. If you want to assign a value of 14 to array position 1, the code would be this: aryNums[1] = 14;
  • 13. • And to assign a value of 36 to array position 2, it's this: aryNums[2] = 36;
  • 14. • Don't forget, because arrays start at 0, the third position in an array has the index number 2. • If you know what values are going to be in the array, you can set them up like this instead: int[ ] aryNums = { 1, 2, 3, 4 }; This method of setting up an array uses curly brackets after the equals sign.
  • 15. • In between the curly brackets, you type out the values that the array will hold. The first value will then be position 0, the second value position 1, and so on. Note that you still need the square brackets after int, but not the new keyword, or the repetition of the data type and square brackets. But this is just for data types of int values, string, and char values. Otherwise, you need the new keyword. So you can do this: String[ ] aryStrings = {"Autumn", "Spring", "Summer", "Winter" };
  • 16. • But not this: boolean[ ] aryBools = {false, true, false, true}; • To set up a boolean array you still need the new keyword: boolean[ ] aryBools = new boolean[ ] {false, true, false, true};
  • 17. • To get at the values held in your array, you type the name of the array followed by an array position in square brackets. Like this: System.out.println( aryNums[2] );
  • 18. • The above code will print out whatever value is held at array position 2 in the array called aryNums. But let's get some coding practice. • Start a new project and call it anything you like. Don't forget to change the name of the Class to something relevant.
  • 19. • Type the following code into your new Main method: