9
And we're back! Welcome to episode 11 of Java Text Tutorials!
Table of Contents:
11. What is an array?
11.1 How do I make an array?
11. What is an array?
An array is a group of data that is stored inside of one variable. Arrays can consist of integers, doubles, strings and so on. An array is useful for running a set of data all at once. For example: You want to have all of a shops's prices inside of one variable so you don't have to call it each time. That is where an array comes useful!
11.1 How do I make an array?
Setup is fairly easy. First you just need to figure out what type of data you will store inside of the array. Let's use int for example. Here is how an int would look like as an array:
int[] anInteger = new int[3];
What does this all mean?
- int[] signifies that you are making an array and you are going to store integers in there.
- anInteger is the name
- new int[3] this might seem confusing. It is declaring that you are making a new array, of type int. The 3 means how much data is going to be stored in there. A computer counts from 0 and up. With a 3 in there, it means that there will be four integers inside of the array.
Now how do you use it? This is stranger. You have to set each value on its own. It is done like this:
anInteger[0] = 1;
This sets the first integer in the array to be equal to one. To do the secod one you would simply do:
anInteger[1] = 2;
And so on until you have all of the data values set. Using it is a bit different than before. We will use something new called a for loop. Don't get too hyped up about it, we will cover that in a later tutorial. But this is how one looks:
for(int i = 0; i < anInteger.length; i++) {
System.out.println(anArray[i]);
}
For now just copy the code for the for loop, it will be covered in a later tutorial.
And there you have it. The system will print all of the numbers that we have set in the array!
In the next tutorial we will talk about if statements.
Table of Contents:
11. What is an array?
11.1 How do I make an array?
11. What is an array?
An array is a group of data that is stored inside of one variable. Arrays can consist of integers, doubles, strings and so on. An array is useful for running a set of data all at once. For example: You want to have all of a shops's prices inside of one variable so you don't have to call it each time. That is where an array comes useful!
11.1 How do I make an array?
Setup is fairly easy. First you just need to figure out what type of data you will store inside of the array. Let's use int for example. Here is how an int would look like as an array:
int[] anInteger = new int[3];
What does this all mean?
- int[] signifies that you are making an array and you are going to store integers in there.
- anInteger is the name
- new int[3] this might seem confusing. It is declaring that you are making a new array, of type int. The 3 means how much data is going to be stored in there. A computer counts from 0 and up. With a 3 in there, it means that there will be four integers inside of the array.
Now how do you use it? This is stranger. You have to set each value on its own. It is done like this:
anInteger[0] = 1;
This sets the first integer in the array to be equal to one. To do the secod one you would simply do:
anInteger[1] = 2;
And so on until you have all of the data values set. Using it is a bit different than before. We will use something new called a for loop. Don't get too hyped up about it, we will cover that in a later tutorial. But this is how one looks:
for(int i = 0; i < anInteger.length; i++) {
System.out.println(anArray[i]);
}
For now just copy the code for the for loop, it will be covered in a later tutorial.
And there you have it. The system will print all of the numbers that we have set in the array!
In the next tutorial we will talk about if statements.
Credit | EgyptianKing for clarification. |
Tags |
3106813
6
Create an account or sign in to comment.
System.out.println(a);
And in order to print out an array, you must loop through each element. You cannot just print the variable anInteger.
for(int i = 0; i < anInteger.length; i++)
{
System.out.println(anInteger(i));
}
System.out.println(anInteger(i));
will throw an exception and is unnecessary. Just use:
System.out.println(i);
Again, thank you for clarifying this, I will make sure that this does not happen in future tutorials.
System.out.println(anInteger(i));
throws an exception. I think you mean
System.out.println(anInteger);
I tested this one out with crazy values just to be sure.
EDIT: PlanetMinecraft doesn't like my brackets. It's an i within brackets after anInteger.
anInteger *open bracket* i *closed bracket*
It's not letting me put the brackets.