Results 1 to 3 of 3
  1. #1
    newToJava3 is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default Array problem please help

    Hi I am trying to create an array say of size 3. What I want this array to hold is another array. So say I have array myArr with indexes 0,1,2 .. In each of these indexes I want there to be another array. Is this possible in java?

    If so how would I create the first array

    whatComesHere[] myArr = new whatComesHere[3];

    thanks

  2. #2
    uoty is offline Member
    Join Date
    Dec 2010
    Location
    Nova Scotia, Canada
    Posts
    2
    Rep Power
    0

    Default

    Hi,
    I think what you're looking for is a two-dimensional array which is indeed possible in java.
    Basically you can create a blank 2d array like so:
    whatComesHere[][] myArr = new whatComesHere[3][3];
    This would create in that array three arrays each with the capacity of three separate values. Also you can create 2d arrays like so:
    whatComesHere [][] myArr = {{1,2,3},{4,5,6},{7,8,9}};
    Hope that helped :)

  3. #3
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    You can also create a so called ragged array, that doesn't have the same length in every row like this:
    Java Code:
    int[][] array = new int[5][];
    for(int i = 0; i < array.length; i++) {
      array[i] = new int[i+1];
    }
    this way you get an array with 5 rows, the first row has 1 element, the second 2 elements and so on.
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Problem with Array Use
    By Mike90 in forum New To Java
    Replies: 1
    Last Post: 06-02-2010, 02:45 PM
  2. array problem
    By jabo in forum New To Java
    Replies: 2
    Last Post: 03-31-2010, 09:54 AM
  3. Array problem
    By c3jcarmy in forum New To Java
    Replies: 11
    Last Post: 03-11-2010, 02:45 AM
  4. Array problem
    By binarzt in forum New To Java
    Replies: 5
    Last Post: 02-14-2010, 09:01 AM
  5. array problem
    By oceansdepth in forum New To Java
    Replies: 3
    Last Post: 04-05-2008, 02:25 AM

Posting Permissions

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