Results 1 to 11 of 11
Thread: Arrays
- 01-10-2011, 02:40 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 14
- Rep Power
- 0
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.
- 01-10-2011, 02:56 PM #2
Member
- Join Date
- Jan 2011
- Posts
- 18
- Rep Power
- 0
you just call the function in main, no need to use println
Java Code:public static void main(String[] args) { getArrayInfo(); } }
- 01-10-2011, 02:57 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 18
- Rep Power
- 0
you just call the function in main, no need to use println
Java Code:public static void main(String[] args) { getArrayInfo(); } }
- 01-10-2011, 03:22 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 14
- Rep Power
- 0
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?
- 01-10-2011, 03:22 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 14
- Rep Power
- 0
- 01-10-2011, 03:36 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 01-10-2011, 03:50 PM #7
Out of topic, but still for the OP,
I hope you have understood why you have been asked to remove the print statementand have it simply call the method.Java Code:System.out.print( 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.Java Code:getArrayInfo();
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-11-2011, 12:39 PM #8
Member
- Join Date
- Nov 2010
- Posts
- 14
- Rep Power
- 0
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.
- 01-11-2011, 12:40 PM #9
Member
- Join Date
- Nov 2010
- Posts
- 14
- Rep Power
- 0
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.
- 01-11-2011, 12:53 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 01-11-2011, 01:09 PM #11
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,
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.Java Code:System.out.println(arrNum[i]);
Hope this makes some sense,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
Similar Threads
-
Using Arrays
By RevGav in forum New To JavaReplies: 2Last Post: 05-25-2010, 03:57 AM -
Help with arrays please.
By ThrashingBoy in forum New To JavaReplies: 2Last Post: 05-05-2010, 12:47 AM -
store array of arrays in array of arrays
By joost_m in forum New To JavaReplies: 4Last Post: 04-19-2010, 10:32 AM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
Need help with 2D arrays...
By rrsv2 in forum New To JavaReplies: 3Last Post: 11-30-2008, 03:15 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks