Results 1 to 11 of 11

Thread: Arrays

  1. #1
    mwenchong is offline Member
    Join Date
    Nov 2010
    Posts
    14
    Rep Power
    0

    Default Arrays

    Hi all.
    I am trying to create an array object. I don't know where am going wrong. After I run the code, I get some unwanted character. Is there a way tha I can get read of those characters?

    code:

    public class ArrayObjectsTwo {

    /** Return list of subscript names (unrealistic; just for demo). */
    public static int[][] getArrayInfo() {
    int info[][];
    info = new int[10][10];
    for (int i=0; i < info.length; i++) {
    for (int j = 0; j < info[i].length; j++) {
    info[i][j] = i;
    System.out.print("" + info[i][j]);
    }
    System.out.println("");
    }
    return info;
    }

    public static void main(String[] args) {
    System.out.print( getArrayInfo());
    }
    }

    code/


    Output:

    0000000000
    1111111111
    2222222222
    3333333333
    4444444444
    5555555555
    6666666666
    7777777777
    8888888888
    9999999999
    [[I@3e25a5 // these are the characters am talking about.
    Process completed.

  2. #2
    charleyjoyce is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    you just call the function in main, no need to use println
    Java Code:
    public static void main(String[] args) {
         getArrayInfo();
        }
    }

  3. #3
    charleyjoyce is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    you just call the function in main, no need to use println
    Java Code:
    public static void main(String[] args) {
         getArrayInfo();
        }
    }

  4. #4
    mwenchong is offline Member
    Join Date
    Nov 2010
    Posts
    14
    Rep Power
    0

    Default

    Thank you very much. I have another similar question that I need to clarify. It involves separating the array class from the main method. How would I go about that considering the code I have already supplied?

  5. #5
    mwenchong is offline Member
    Join Date
    Nov 2010
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by charleyjoyce View Post
    you just call the function in main, no need to use println
    Java Code:
    public static void main(String[] args) {
         getArrayInfo();
        }
    }
    Thank you very much. I have another similar question that I need to clarify. It involves separating the array class from the main method. How would I go about that considering the code I have already supplied?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default

    Move the main() method into a Main class (or whatever name you want to call it)?
    If it's in a different package to the ArrayObjectsTwo class then you'll have to import that class into Main.

  7. #7
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Out of topic, but still for the OP,

    I hope you have understood why you have been asked to remove the print statement
    Java Code:
    System.out.print( getArrayInfo());
    and have it simply call the method.
    Java Code:
    getArrayInfo();
    Your getArrayInfo() is actually returning the object [return info] of the multi-dimensional array you have defined. And it was the object's string representation that was getting displayed as those weird characters at the end of your output.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  8. #8
    mwenchong is offline Member
    Join Date
    Nov 2010
    Posts
    14
    Rep Power
    0

    Default Arrays

    Thank you very much. I have trying to teach myself java for some time now and its with this kind of assistance the will make me achieve my Java learning goal. I really appreciate this. I need you to have a look at the next code and tell me where I have gone wrong. I have created a class with an array and a main method to run the class. I still get funny characters printed instead of the required characters. Please help

    code:
    /*
    * The class that is holding the array
    */
    public class Pots {
    int arrNum[];

    public int[] runArr() {
    arrNum = new int[5];
    for(int i : arrNum)
    arrNum[i] = i;
    System.out.println(arrNum);
    return arrNum;
    }
    public int[] getArrNum(int numArr[]) {
    arrNum = numArr;
    return arrNum;
    }

    }

    code/


    code:

    /*
    * the main method
    */

    public class PotsDemo {
    public static void main(String args[]) {
    Pots mine = new Pots();
    mine.runArr();


    }
    }

    code/


    Please help me correct this code.

  9. #9
    mwenchong is offline Member
    Join Date
    Nov 2010
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by goldest View Post
    Out of topic, but still for the OP,

    I hope you have understood why you have been asked to remove the print statement
    Java Code:
    System.out.print( getArrayInfo());
    and have it simply call the method.
    Java Code:
    getArrayInfo();
    Your getArrayInfo() is actually returning the object [return info] of the multi-dimensional array you have defined. And it was the object's string representation that was getting displayed as those weird characters at the end of your output.

    Goldest
    Thank you very much. I have trying to teach myself java for some time now and its with this kind of assistance the will make me achieve my Java learning goal. I really appreciate this. I need you to have a look at the next code and tell me where I have gone wrong. I have created a class with an array and a main method to run the class. I still get funny characters printed instead of the required characters. Please help

    code:
    /*
    * The class that is holding the array
    */
    public class Pots {
    int arrNum[];

    public int[] runArr() {
    arrNum = new int[5];
    for(int i : arrNum)
    arrNum[i] = i;
    System.out.println(arrNum);
    return arrNum;
    }
    public int[] getArrNum(int numArr[]) {
    arrNum = numArr;
    return arrNum;
    }

    }

    code/


    code:

    /*
    * the main method
    */

    public class PotsDemo {
    public static void main(String args[]) {
    Pots mine = new Pots();
    mine.runArr();


    }
    }

    code/


    Please help me correct this code.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default

    System.out.println(arrNum);

    It's not funny numbers, it is the output of this line.
    [[I@3e25a5 translates as:
    [[I - int array.
    @ - at
    3e25a5 - address.

  11. #11
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    You are again trying to print the "instance variable". And that will always print whatever you have received.

    Again, you have declared the array, but you haven't inserted anything inside that. So eventually you will end up with all 0 values whenever you will print the contents.

    Use array brackets with index number inside it when you are planning to print the contents. For e.g. use,

    Java Code:
    System.out.println(arrNum[i]);
    Plus, use proper starting and closing braces in your "for" loop, or else by the time you will reach the print statement, your index variable 'i' would be out of scope.

    Hope this makes some sense,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. Using Arrays
    By RevGav in forum New To Java
    Replies: 2
    Last Post: 05-25-2010, 03:57 AM
  2. Help with arrays please.
    By ThrashingBoy in forum New To Java
    Replies: 2
    Last Post: 05-05-2010, 12:47 AM
  3. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  4. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  5. Need help with 2D arrays...
    By rrsv2 in forum New To Java
    Replies: 3
    Last Post: 11-30-2008, 03:15 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
  •