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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 4 of 4

Thread: array as parameter

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default array as parameter

    Is there a way to make sure only a 3x3 array is passed into a method? I am trying to create a matrix class and a 3x3 matrix is a special case that can have the determine calculated, in terms of runtime, very quickly.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: array as parameter

    Yes. Arrays have a length property.

    public class ArrayLengthEg {
        public static void main(String[] args) {
            int[] arr = new int[42];
            System.out.println("Length is " + arr.length);
        }
    }

    You could check that the array you are passed has length 3. You will also have to check that each element (which is also an array) also has length 3.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: array as parameter

    Sorry, I am going to make a math package, so I want to pass a 2d array and it can choose the fastest alforithm to use. The standard algorithm is very slow and matrix around 10x10 takes over a second. People who are building simulators that is not good for them. I want to make it as simple to use, so they don't have to search for the fastest they just call inverse() and pass a matrix through and have method overloading but with matrix size.

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: array as parameter

    You can't overload methods based on the length of an array argument. Although an unchanging property of any given array, the length of an array is not part of its type.

    Also although arrays can be interpreted as 2D matrices, Java arrays do not have a "dimension" in quite that way. A variable declared as double[][] is nothing but an array of double[] elements: that is, an array of arrays. These array elements can be interpreted as rows in which case the length property of the array argument will be the number of rows. Nothing, however, requires that these rows all have the same length. (or have any length at all - they may be null). In other words being a (square) 3x3 matrix is not something that is expressible as an array type. And because you can't express it as a type you can't use method overloading to distinguish between different inverse() methods.

    What you can do is what I indicated before: have a single inverse() methods that looks at its array argument and responds appropriately. Where "appropriately" could be (a) calling a special case for small square matrices (b) calling some other method for larger matrices (c) throwing an IllegalArgumentException if it is not passed a square matrix at all.

  5. The Following User Says Thank You to pbrockway2 For This Useful Post:

    pjo (March 16th, 2012)

Similar Threads

  1. constructor with an arraylist parameter?
    By ronimacarroni in forum Object Oriented Programming
    Replies: 5
    Last Post: March 5th, 2012, 04:42 PM
  2. [SOLVED] sending a parameter to a new JDialog
    By luisp88 in forum AWT / Java Swing
    Replies: 4
    Last Post: October 5th, 2011, 09:31 AM
  3. [SOLVED] Send and Receive Parameter
    By java_mein in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: May 14th, 2010, 02:22 PM
  4. Initialization parameter of servlets
    By rakesh in forum Java Servlet
    Replies: 3
    Last Post: April 13th, 2010, 03:14 AM
  5. How do you pass an Array as a Parameter?
    By Arius in forum Java Theory & Questions
    Replies: 1
    Last Post: January 23rd, 2010, 09:36 PM

Tags for this Thread