+ Reply to Thread
Results 1 to 9 of 9

Thread: Java:Tutorial - Arrays

  1. #1
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Java:Tutorial - Arrays

    In an earlier tutorial, we looked at how to store a single value to a variable, similarly an array is a named place to store a SET of values allowing you to group common types.

    Sometimes, it becomes necessary to order variables in a list to spot trends:

    Temperature on Sept 1 = 90
    Temperature on Sept 2 = 87
    Temperature on Sept 3 = 84
    This is the ideal application for an array. All the variables and values are of the same type, integers, string, or what ever. Moreover there order and place in the list is important. Since an array allows you to store a “list” of values as a single “variable,” there must be a means of referencing each item. This reference is called an index. So therefore:

    If
    Code:
    Temp[1] = 90
    Temp[2] = 87
    Temp[3] = 84
    Then
    Code:
    int x = 2;
    Temp[ x ] = 87;
    At first, a concept that is difficult to grasp is that the index starts at 0. There for, an array with 5 elements contains the index values {0, 1, 2, 3, 4} but how do we make an array?


    Code:
    int z[ ] = new int[5];
    A working example might look like this:
    Code:
    int x;
    int z[] = new int[10];
    for (x = 0; x < 10; x++) {
    z[x] = x;
    }
    Therefore z[4] = 4! Arrays do not have to be numbers, for example:

    Code:
     int x;
    String myArray[ ] = new String[5];
    myArray[0] = "hello";
    myArray[1] = "this";
    myArray[2] = "is";
    myArray[3] = "an";
    myArray[4] = "array";
    for (x=0; x <= 4; x=x+1  )
    {
    System.out.println( myArray[x] );
    }
    You can accomplish the same thing in less lines of code by using the following:
    Code:
    int x;
    String myArray[] = {"hello", "this", "is", "an", "array"};
    for (x=0; x<=4; x++){
    System.out.println( myArray[x] );
    }
    If a single list of values isnt powerfull enough, we also have the ablilty to use miltidimensional arrays. This is sometimes refrered to as a matrix. Each index has its own set of indicies that are pared with it, for example:

    Code:
    int matrix[][] = {{1,2,3,4}, {4,5,6,7}, {7,8,9,10}};
    for (int i=0; i<=2; i++){
    for (int j=0; j<=3; j++){
    System.out.println( matrix[i][j] );
    }
    }

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,851
    Blog Entries
    4
    Rep Power
    49

    Smile Re: Java:Tutorial - Arrays

    I just had to reply to this wonderful thread!

    I mean Wow, normally people just make random explanations with no real sense; But you made one with quite awsome style, thank you for this great thread.

  4. #3
    Maze's Avatar
    Maze is offline Learning Programmer
    Join Date
    Dec 2008
    Posts
    32
    Rep Power
    0

    Re: Java:Tutorial - Arrays

    Thanks for nice share. It was really good.

    I need some more examples about multi dimensional arrays. Could you explain and put some more examples about?

    THanks

  5. #4
    Jordan Guest

    Re: Java:Tutorial - Arrays

    I've never seen this tutorial or at least I cannot ever remember reading it. Very nice, thank you. +rep

  6. #5
    miromtm's Avatar
    miromtm is offline Newbie
    Join Date
    Nov 2008
    Location
    CAIRO
    Posts
    18
    Rep Power
    0

    Re: Java:Tutorial - Arrays

    WOW , man am very thankful ,

  7. #6
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,851
    Blog Entries
    4
    Rep Power
    49

    Re: Java:Tutorial - Arrays

    Quote Originally Posted by miromtm View Post
    WOW , man am very thankful ,
    Good to see happy faces

  8. #7
    whitey6993's Avatar
    whitey6993 is offline Programming Expert
    Join Date
    Dec 2008
    Posts
    435
    Rep Power
    15

    Re: Java:Tutorial - Arrays

    Excellent tutorial for introducing arrays and multi-dimensional arrays.

  9. #8
    Aroos is offline Newbie
    Join Date
    Jun 2009
    Posts
    1
    Rep Power
    0

    Post java:Tutorial

    Thanks for nice share. It was really good.

    I need some more examples about multi dimensional arrays java . Could you explain and put some more examples about?

    THanks

  10. #9
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,851
    Blog Entries
    4
    Rep Power
    49

    Re: java:Tutorial

    Quote Originally Posted by Aroos View Post
    Thanks for nice share. It was really good.

    I need some more examples about multi dimensional arrays java . Could you explain and put some more examples about?

    THanks
    Sure, just view this thread, it has great information about multi dimensional arrays !
    Multidimensional Arrays
    Credit goes to Chili5

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to copy arrays in Java?
    By AnnexTrunks in forum Java Help
    Replies: 7
    Last Post: 10-24-2011, 07:52 PM
  2. Beginner Tutorial: arrays in ruby
    By mr mike in forum Ruby on RailsTutorials
    Replies: 0
    Last Post: 12-23-2010, 12:44 PM
  3. Strings and Arrays Tutorial
    By hodge-podge in forum Perl
    Replies: 2
    Last Post: 05-06-2010, 06:08 PM
  4. C# Tutorial: Arrays
    By CommittedC0der in forum CSharp Tutorials
    Replies: 6
    Last Post: 03-30-2010, 03:14 PM
  5. JavaScript:Tutorial, Using Arrays
    By TcM in forum JavaScript Tutorials
    Replies: 25
    Last Post: 07-27-2009, 04:13 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts