Results 1 to 5 of 5

Thread: Arrays made easy

  1. #1
    Senior Member
    Join Date
    Aug 2001
    Posts
    259

    Arrays made easy

    I’m writing this tutorial with help from Java Second solutions by John Lewis and William Loftus, most of it is from me but a few of the definitions are from the book.

    What an array does: An array is a way of indexing an holding a large sum of information that can be called, used and sorted.

    Declaring and Initializing an array: for an array to work you need to initialize it to a certain constant for example int[] height = new int[11]; . This will create a 1 dimensional array that holds 11 spots for integers. Integer arrays can only hold numbers, string arrays can hold numbers and letters and words but you can’t do addition with the string arrays.

    Visualization:
    1D array: A 1 dimensional array is an array that’s just one column and this is usually used for sorting simple information like sorting.

    2D array: A 2 dimensional array is like an excel table you have x amount of columns and x amount of rows that can be pulled by going down the rows and the columns and putting information in those slots. for example int[] 2dheight = new int[5][11]; this will hold 55 spots that can be dedicated to integers. With the 5 rows and 11 columns. This is the most common array especially for passwords.

    3D array: A 3 dimensional array can be thought of as a cube. With the first digit being a specific part like if you made a 3 d string array that holds world info like if you made an array string[] country = new string[10][5][11]; would allow you to go thru any part of the first like the 10 would be countries and then from there you could go to 5 regional provinces and like 11 cities.

    ND array: Any array with N dimensions should be thought of a bunch of lesser arrays, like visualize a 4 d array just as a bunch of limited 3 dimensional arrays, if it helps visualize the arrays in space like a bunch of interconnected 3d arrays (or cubes if you will) connected by some common thread.

    Sorting made easy: Sort an array by descending or assending order with integers you should use a comparision statement like(don’t mind the syntax errors or the lack of variable declarations)while (x < arraycounter){ arraycounter = 0; int x = 0; if {(array[1][x] > array[1][x + 1]); temp = array [1][x];array[1][x] = array[1][x + 1]; array[1][x + 1] = temp;} x++;}

    searching an array: use a comparison statement with this too. Cout << “enter search variable”; Cin >> variable while (h > arraycounter2){ while (x < arraycounter) { if (array [1][x] == variable){ cout >> array[1][x];}
    x++;}h++}
    Alternate realities celebrate reality. If you cant handle the reality your in, then you wont be able to handle the one your attempting to escape to.

  2. #2
    Junior Member
    Join Date
    Jul 2002
    Posts
    1
    Thanks, I am just starting in on Java and the arrays were confusing me a little. This helped.

  3. #3
    Senior Member
    Join Date
    Sep 2001
    Posts
    1,027
    Just to make things clear, your examples are in C++...
    In java, multidimensionnal arrays are actually arrays of arrays...
    Each level of array must be declared/initialized...

    Ammo
    Credit travels up, blame travels down -- The Boss

  4. #4
    Senior Member
    Join Date
    Aug 2001
    Posts
    259
    yeah, it's easyer to declare arrays in C++ but the java book I use has a good explination so I paraphrased it for the part on defining the array. You'd need to use a vector in java.
    here's an example of a vector:

    //jebus.java Author: zepherin
    //help from Java Software Solutions 2nd edition by John Lewis and William Loftus
    //this will help the undstanding of vectors
    import java.util.Vector;
    public class jebus
    {
    public static void main (String[] args)
    {
    Vector animal = new Vector(); //this is the intialize of the vector

    animal.addElement ("Bear"); //this adds parts to the vector
    animal.addElement ("Cat");
    animal.addElement ("Dog");

    System.out.print1n (animal); //this prints the hole vector
    System.out.print1n (At index 1: " + animal.elementAt(1)); //this prints the animal //at index 1

    System.out.print1n ("Number of animals:" + animal.size()); //this prints the //number of animals
    }
    }
    Alternate realities celebrate reality. If you cant handle the reality your in, then you wont be able to handle the one your attempting to escape to.

  5. #5
    Senior Member
    Join Date
    Sep 2001
    Posts
    1,027
    Vectors are actually "dynamic" arrays... (also somewhat deprecated in favor of Collection derivatives).

    You can still use plain arrays in java but declaration is slightly diffrent for single dimension arrays and much diffrent for multidimensionnal arrays as you have to re-declare arrays in each subscript of the first array...

    Ammo
    Credit travels up, blame travels down -- The Boss

Posting Permissions

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