Minecraft Blogs / Tutorial

Java Text Tutorial 11 - Arrays

  • 408 views, 1 today
  • 1
  • 0
  • 7
Cool_Man7's Avatar Cool_Man7
Level 16 : Journeyman Modder
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.
CreditEgyptianKing for clarification.
Tags

Create an account or sign in to comment.

Lambdastic
10/13/2014 10:56 pm
Level 7 : Apprentice Warrior
Lambdastic's Avatar
I believe that this line is a mistake.
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));
}
1
Cool_Man7
10/14/2014 4:14 pm
Level 16 : Journeyman Modder
Cool_Man7's Avatar
Ah yes, it is. I must have been writing this in a rush and made a mistake. I normally test these before doing them to be safe just in the event I did make such a silly mistake and this one was missed. Thank you for clarifying this, I will update the tutorial accordingly. Although a side note using
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.
1
Lambdastic
10/14/2014 5:54 pm
Level 7 : Apprentice Warrior
Lambdastic's Avatar
Using System.out.println(i) will print a number from 0 to the size of the array on each iteration, not the element.
1
Cool_Man7
10/14/2014 6:11 pm
Level 16 : Journeyman Modder
Cool_Man7's Avatar
Ah yes sorry again. My values when I tested just to be sure were 0 and 1 and that is exactly how it printed. I've never tried printing arrays before to be honest. However your idea doesn't seem to make sense.
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.
1
Lambdastic
10/14/2014 6:17 pm
Level 7 : Apprentice Warrior
Lambdastic's Avatar
My bad

anInteger *open bracket* i *closed bracket*


It's not letting me put the brackets.
1
Cool_Man7
10/14/2014 6:20 pm
Level 16 : Journeyman Modder
Cool_Man7's Avatar
Yes, yes. Same happened to me. I suppose that is what you were trying to do the whole time ;)
1
Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome