Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Unexpected Output from a 2D Array

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 03-12-2012, 04:18 AM PM User | #1 b00mtastik New to the CF scene   Join Date: Mar 2012 Location: In your shadow. Posts: 1 Thanks: 0 Thanked 0 Times in 0 Posts Unexpected Output from a 2D Array Hey guys, B00mer here. So, was just coding and messing around with 2 dimensional arrays to kinda get a better understanding of it and also to be able to manipulate it and do some other stuff with it. However, upon compiling and running the 2D array to output all its contents after it had already been populated during compile, it game me some crazy numbers, instead of the ones I was looking for. And I for the life of me cannot understand why. So I thought you guys might be able to help. The simple code is as follows: Code: ```public class nbyn { public static void main(String[] args) { //Array Box A int [][] a = new int[][]{ { '1', '2', '3' }, { '4', '5', '6' }, { '7', '8', '9' }, }; //Array Box B - Ignore this one for now, nothing happening with it int [][] b = new int[][]{ { '1', '2', '3' }, { '4', '5', '6' }, { '7', '8', '9' }, }; int i, j; //traverse the array 'a' and output all its contents for (i=0; i < a.length; i++) { for (j=0; j < a[i].length; j++) { System.out.print(" " + a[i][j]); } System.out.println(""); } } }``` So the output I was expecting was: Code: `1, 2, 3, 4, 5, 6, 7, 8, 9` Instead I get this: Code: ```run: 49 50 51 52 53 54 55 56 57 BUILD SUCCESSFUL (total time: 0 seconds)``` Any explanation of how this output came to being, and also a bit of help on what I NEED to do in order to get the output I want, would be amazingtastik. Danke. -B00mer
 03-12-2012, 05:14 AM PM User | #2 Fou-Lu God Emperor     Join Date: Sep 2002 Location: Saskatoon, Saskatchewan Posts: 16,599 Thanks: 4 Thanked 2,598 Times in 2,567 Posts Those int representations for chars. 1 - 9 is 49 - 57 inclusive.
 03-12-2012, 02:37 PM PM User | #3 alykins Senior Coder     Join Date: Apr 2011 Posts: 1,608 Thanks: 37 Thanked 183 Times in 182 Posts this ascii chart might help you understand better- look at the red 1,2,3... etc and then look at the Dec value of it- you are getting the conversion of the char-> int ... I am surprised though that it did not error out on a bad type __________________ I code C hash-tag .Net Reference: W3C W3CWiki .Net Lib Validate: html CSS Debug: Chrome FireFox IE
 03-12-2012, 05:27 PM PM User | #4 Gox Regular Coder     Join Date: May 2006 Location: Ontario, Canada Posts: 392 Thanks: 2 Thanked 20 Times in 20 Posts I believe you need to remove the single quotes in your array instantiation. i.e. Code: ```int[] anArray = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 };```
 03-12-2012, 06:55 PM PM User | #5 Fou-Lu God Emperor     Join Date: Sep 2002 Location: Saskatoon, Saskatchewan Posts: 16,599 Thanks: 4 Thanked 2,598 Times in 2,567 Posts It won't error out since its not technically a bad type. A char is just a byte, and a byte is a lower "quality" primitive than the int. So you can always stick a lower quality primitive into a "higher" datatype, and more often than not without an explicit conversion. Effectively, if you can stuff a value into a variable without losing precision, it will attempt to implicitly cast the value to match it. Byte -> Short Int -> Int -> long Int -> float -> double, but you cannot go the other way. This is the same behaviour that C follows.

 Bookmarks

 Tags 2d array, arrays, java, traversing arrays

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 02:21 AM.