How to - Arrays!

HOW TO USE ARRAYS!


Arrays are very simple, don't be frightened! They're basically a faster way to produce variable declarations. And often times, the only way! It's very fundamental stuff, so learn it up!


-Formatting a normal int variable declaration goes like this:
int theNameOfTheIntVariable = 9;
    -But you already knew that, right?


    -Well now with arrays you do something a little different. To declare the array and it's
    -name, you just add some pretty brackets.


int[ ] HalloIAmAnArrayName =
    -And then you'll type after the equals sign, something a bit different than a normal variable.
    -You don't type the variable's value right there with an array.
    -You type the NUMBER of values. Because an array contains MANY values! 
    -That's the point!


So your array declaration will look like this when completed:
int [ ] HalloIAmAnArrayName = new int[5];


    -The [5] declares to the array that the array HalloIAmAnArrayName will contain 6 different
    -values.
    -Yes, 6. Not 5. Because when you go to declare your array values via manual entry, the 0 
    -index will always be there. 


  Here's how you declare your array's values:
HalloIAmAnArrayName[0] = 415;
HalloIAmAnArrayName[1] = 415;
HalloIAmAnArrayName[2] = 415;
HalloIAmAnArrayName[3] = 415;
HalloIAmAnArrayName[4] = 415;
HalloIAmAnArrayName[5] = 415;


Alright! We're all done declaring our array and her values!
Now let's do a little utilization!


System.out.println("Hi, the fifth index of my array has a value of:"+HalloIAmAnArrayName[5]);


For a step by step video explanation and sexy audio, just look at the...
DETAILED VIDEO BELOW



6 comments:

  1. So if I was making a text adventure game and I wanted to allow the player to go to different locations (barn, chicken coop, and house) by typing "go to house" or whatever, could I use an array to allow the player to choose which set of println commands are displayed? Because I'm trying to do this right now:
    (declare "move" as a string in the begining)

    move = Keyboard.readString()
    if (move.equals("barn"));
    System.out.println("You walk to the barn.");

    if (move.equals("chicken coop"));
    System.out.println("You walk to the coop.:);

    so I have these if statements but no matter what the person types, the program displays all the messages no matter what. Like if I were to type "go to barn", this is what would pop up:

    You walk to the barn
    You walk to the coop

    ReplyDelete
  2. You could, but since you're going to parse their response with an if to check if it equals an answer and print to the screen, I think it's not necessary to use an array in this instance.

    I'm not sure what problem you're having with the code snippet, but I wrote you a working one. Responses are case sensitive!

    import javax.swing.*;
    public class BlankPaper {


    public static void main(String[] args) {
    String response = JOptionPane.showInputDialog(null, "What path would you like to take?"
    + "\nChicken coop"
    + "\nBarn" );
    if(response.equals("Chicken coop")){
    System.out.println("You go to the chicken coop");

    }else if(response.equals("Barn")){
    System.out.println("You go to the barn");
    }



    }
    }

    ReplyDelete
  3. you could make it so it's not case sensitive in if statement
    if(response.equals.IgnoreCase("Chicken coop")){
    System.out.println("You go to the chicken coop");

    }else if(response.equalsIgnoreCase("Barn")){
    System.out.println("You go to the barn");
    }

    ReplyDelete
    Replies
    1. Also the Video Does not work =(

      Delete
  4. how sexy is the audio?

    ReplyDelete