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: returning a 2D array

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default returning a 2D array

    hi so fare i have this but it doesnt seem to be working:

    in one class i have:
    public class siwtchMenu{
     
    public String[][] arrayA(int x, int y){
            String arrayTest[][] = {{"0", "1", "2"},{"0","1", "2"}};
            return arrayTest[x][y]; //error is here
        }
    }
    and then in another class i want to call arrayA:
    public class case1{
     
    void case1(){
            switchMenu obj = new switchMenu();
     
            System.out.println("Array is:");
     
            for(int i=0; i<3; i++){
                    System.out.println(obj.arrayTest[i][i]+ ", ");
                 }
          }
    }

    when i do this it comes up with an error (at error is here) saying:

    incompatible types
    required: java.lang.String[][]
    found: java.lang.String
    Please help me on this as i have know idea what im doing wrong!
    thanks in advance! =]
    Last edited by straw; March 11th, 2010 at 11:38 AM. Reason: miss type


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: returning a 2D array

    For your class SwitchMenu, you have defined the function arrayA to return a 2D array, but you actually just return a String value. There is also an error in your case1 with how you access the first class...you've defined a method in the first class but try to access a variable that does not exist from the case1 method. The solutions depend upon what behavior you want, here's a fix to the first class.

    public class siwtchMenu{
     
    public String[][] arrayA(int x, int y){
            String arrayTest[][] = {{"0", "1", "2"},{"0","1", "2"}};
            return arrayTest; 
        }
    }

    Now you can correctly access the arrayA by calling that function and storing the returned 2D array in a local. As another side note, you can define your arrayTest as an object variable rather than redefining the 2D array for every call to arrayA

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: returning a 2D array

    hi thanks for the quick responce, now re the case1 error that was me just being tired and typing it out wrong i didnt do that in my actual project!

    when i do as you say above it just comes up with:

    [[Ljava.lang.String;@130c19b
    rather than the array

    so how would i go about defining arrayTest as an object variable?
    baring in mind that i do want to edit the array latter!

  4. #4
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: returning a 2D array

    OK there are two problems I can see here first of all here:
    public class siwtchMenu{
     
    public String[][] arrayA(int x, int y){
            String arrayTest[][] = {{"0", "1", "2"},{"0","1", "2"}};
            return arrayTest[x][y]; //error is here
        }
    }

    When you say pulic String[][] in that code you are saying you want to return a 2D array of strings but by saying arrayTest[x][y] you are only returning the string in the position [x][y]. Does that make sense to you? What you need to do is just replace String[][] with String, I believe that should work. However you haven't got the loop logic in case1 correct I think you should be able to figure it out once you get some feedback from the program with that change.

    EDIT:

    Ah just looked at your code again and maybe I misunderstood what you are trying to do. I assumed you want the loop in case1 to run through and call the method arrayA each time to return the value contained in it. What you are trying to do here :
    System.out.println(obj.arrayTest[1][1]+ ", ");
    is ask for the value of a variable in the class(which doesn't exist as copeg said). If my assumption is right you will want to change this to a method call. Otherwise you could declare the array as a public global variable or use the method to pass the whole array to the case1 class and store it in a new array there and loop through that one. Is any of this making sense? Let us know exactly what you want each method in each class to do.
    Last edited by Faz; March 11th, 2010 at 04:38 AM.

Similar Threads

  1. Arrays.equals not returning correct answer.
    By Anyone in forum Collections and Generics
    Replies: 2
    Last Post: February 11th, 2010, 10:12 AM
  2. Object array to int array?
    By rsala004 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2009, 04:09 AM
  3. throwing and returning
    By chronoz13 in forum Exceptions
    Replies: 6
    Last Post: October 25th, 2009, 12:00 AM
  4. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 02:14 PM
  5. Returning Random Strings from an Array
    By cfmonster in forum Collections and Generics
    Replies: 3
    Last Post: September 8th, 2009, 11:13 PM