• Post Reply Bookmark Topic Watch Topic
  • New Topic

Am I doing this right with Arrays?  RSS feed

 
Daniel Dagenhart
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IM still very new to Java.
I been watching some videos about Arrays on You tube.
What I am trying to do is make an Array 5x5  filled with numbers 0 to 24 .

I want to keep this simple and not use commands that are more advanced. This is basic  of basic..

I have this, it compiles, but I cant get it to print anything..



I tried rc in the println and N, but both of them seem empty.  Any help would be great in helping me understand arrays a bit more.
Thank you.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have not given a value to any elements of the array "n", nor have you initialized any elements of array "rc".
 
Daniel Dagenhart
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel like I need another variable int a=1  then increase the numbers by 1 each time it loops/
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that Java8 is here you would try something different:-The range(...) method creates an IntStream object which will go from (in this case 10 to 14), and then the toArray() method turns that into an array. It is awkward to print arrays because they don't override the toString() method, but there are methods in the Arrays class which display any kind of array (except char[]s) in this sort of format:-
java ArrayFillDemo
[10, 11, 12, 13, 14]
That is one way to fill an array with consecutive numbers. Another way to fill an array is to use the fill method in the Arrays class, which fills the array with multiple copies of the same thing.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't quite see the purpose of the 'rc' array. Array 'n' with your proposed variable 'a' should suffice.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your loop construct is not idiomatic in Java. This is how it's usually expected to be done:

I can't even begin to understand what you're trying to do on line 10 of your posted code but it certainly isn't filling the array with numbers from 0 - 25.

Note how this code is resilient to any changes you may make to the array size. You can change the dimensions of the array to anything else and the loop headers would not need to be changed one bit. Your version was very brittle in that changes to the array's size would need to be reflected in your loop's termination expressions.
 
tushar attar
Ranch Hand
Posts: 39
Chrome Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have changed your code a bit with better implementation. Have a look at it and try to understand by yourself what is being done

 
tushar attar
Ranch Hand
Posts: 39
Chrome Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried rc in the println and N, but both of them seem empty.  Any help would be great in helping me understand arrays a bit more.


First of all nothing is printing because, you haven't passed anything to System.out.println();

If you pass rc or N in println it will pass some garbage value instead of data as you havent passed any index in them.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tushar attar wrote:. . . If you pass rc or N in println it will pass some garbage value instead of data as you havent passed any index in them.
What does that mean? You can never get garbage values from System.out.println(). I think you have misunderstood what is displayed.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tushar attar wrote:I have changed your code a bit with better implementation. Have a look at it and try to understand by yourself what is being done


That won't help OP much, especially since it won't even compile. On line 5, rc is declared as int[] but on line 11, you use it like it's a nested array. The compiler will reject that. Also, you didn't change the non-idiomatic use of the for-loops, which I already pointed out to OP earlier. Lines 10-12 also has a bug that is hidden by the code's poor formatting and inconsistent indentation. Lastly, line 12 is actually unnecessary. You could have just put count++ on the left right side of the assignment statement above it, assuming you fixed the first problem I pointed out. Deleting line 12 and modifying line 11 actually gets rid of the bug in that section of the code.

If you want to use System.out.println() to see the contents of an array, pass it Arrays.toString(arr) to get human-friendly output. Just passing an array reference will display whatever String.valueOf(arr) will give back, which isn't, as you said, "just garbage."

BTW, if you try to use Arrays.toString(arr) when arr is declared as an array of arrays as is the array int n[][] in OP's code, it still won't be human-readable. In this case, use Arrays.deepToString(n) instead to see what is in array n.

And one final thing: line 7 where you initialize count = 1 will NOT result in what OP wanted, which was to fill the 5x5 int array "with numbers 0 to 24".  Line 7 is also unnecessary. You can initialize multiple int variables in the for-loop header, so you can do this:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!