Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: 1D Sub-Array From 3D Array of Doubles

  1. #1
    Junior Member mblem22's Avatar
    Join Date
    Jun 2012
    Thanked 0 Times in 0 Posts

    Default 1D Sub-Array From 3D Array of Doubles

    Hello -

    Wondering how I can get a reference to a 1D array which is part of a 3D array of doubles, so that I can pass it to a method. What I mean is basically this:

    double myDouble = myFunction(myArray[i][ALL][j]);

    where myFunction takes a 1D array of doubles as an argument. If you are familiar with MATLAB, I basically want the java equivalent of myArray(i)((j).

    Is there a built in method somewhere for doing this? Or do I need to make a new array? I would hate to pointlessly use more memory.

    Sorry if this is not clear (very tired...). I'd be happy to clarify. Thanks for any help. This one has me quite stumped.

    (This thread is also posted at 1D Sub-Array From 3D Array of Objects)

  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    In Hyrule enjoying a chat with Demise and Ganondorf
    My Mood
    Thanked 48 Times in 42 Posts

    Default Re: 1D Sub-Array From 3D Array of Doubles

    Here's a way to think about it: every time you use brackets to refer to an element in an array, the number of dimensions is reduced by one.

    Look at this code:

    int[][] numbers = {{1, 2}, {3, 4}};
    int[] nums = numbers[1];

    Because numbers is an array of arrays, if we refer to one of it's elements, an array is returned. We started with a 2D array (numbers), then got a 1D array by referencing an element in the 2D array.

    Following this logic, to get a 1D array from a 3D array, the general syntax would be:

    oneDArray = threeDArray[indexA][indexB];
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!

    Wanna hear something funny?

    Me too.

Similar Threads

  1. Array List of Array Lists working for first item but not for second.
    By javapenguin in forum Collections and Generics
    Replies: 6
    Last Post: February 15th, 2012, 04:12 PM
  2. Replies: 4
    Last Post: November 14th, 2011, 09:00 PM
  3. Doubling The Array Size And Randomizing Array Return
    By Pingu00 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 27th, 2011, 10:50 AM
  4. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  5. Replies: 2
    Last Post: May 6th, 2011, 05:19 PM