Advertisement

[Java] Initializing And Populating 2D Array Example

Started by February 16, 2014 11:24 PM
10 comments, last by _mark_ 10 years, 8 months ago

Unfortunately I do not see anywhere in these forums to post code snippets, so I will park this here.

The following code example shows how to initialize, populate, and output a 2D array in Java.


public class Main {
	public static void main(String[] args){
		int num = 50;
		int[][] _2D_Array = new int[num][num];
		int x,y;
		for (x = 0 ; x < num; x ++){
			for (y = 0; y < num ; y ++){
				_2D_Array[x][y] = 0;
				System.out.print("" + _2D_Array[x][y]);
			}
			System.out.println(" ( " + x + " )");
		}
		System.out.println("\n *** Done *** \n");
		}
	}

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson


Unfortunately I do not see anywhere in these forums to post code snippets

You can write an article, "Java tips from Shippou".

Stay gold, Pony Boy.
Advertisement


Unfortunately I do not see anywhere in these forums to post code snippets

You can write an article, "Java tips from Shippou".

And promptly get rejected ... I read the article guidelines sad.png

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

Hmm, there should be some kind of a suggestions board on the forum, where you can post random tips about whatever you want.

Stay gold, Pony Boy.
This is a discussion forum, I'm not sure that such posts are great discussion starters. Something like that might be better placed in your Journal.

That said, I don't think that is a great example:
  • Your code has no discernible naming convention
  • Java arrays are already zeroed when allocated
  • The loop counters should be local to the loops
  • You are mixing I/O in with zeroing the array
  • You have an unnecessary string concatenation, which is confusing
  • Your dimensions are so big that the output doesn't fit into a reasonable space
  • By having both dimensions the same, you miss an opportunity to demonstrate how to handle arrays with differing width and height values
  • By filling the array with uniform values, you miss an opportunity to demonstrate the array indices visually

He put an underscore prefix in the array name because Java disallows variable names starting with numbers :D

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

Advertisement
i'm confused on the purpose of this post. it's nothing that isn't covered when learning the basics of a language, doesn't do anything interesting that'd be helpful to warrant itself as a code snippet. what exactly made you think this post would be helpful.

imo if we need a place for coding snippets, i think coding horrors is will suited for this purpose. coding horrors doesn't have to be exclusive to terrible things in programming.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

I've seen a "Code Sharing" subforum in Java-Gaming.org. Intellectual property paranoia aside, I've seen it is quite useful.

For example, everybody knows libGDX right? Pretty popular lib. Quite a few of its "fast math" implementations (ceil, floor, LUT for cosine/sine, etc) came from the Code Sharing subforum, the MapedObject library that comes with LWJGL started there, and so on.

Its stuff that alone doesn't merits a whole lib, and that it could be very useful for many people nevertheless (and that could use some "peer reviewing" by our forum members).

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

i'm confused on the purpose of this post. it's nothing that isn't covered when learning the basics of a language, doesn't do anything interesting that'd be helpful to warrant itself as a code snippet. what exactly made you think this post would be helpful.

Google Java 2D Array

Lots of hits - yet a lot of them are not "quite right" - no one actually gives a simplified code example on how to do all three common operations of a multidimensional array.

I learn by example - and get frustrated when there are no "decent & simple" examples that show how to do what I am after.

After the many years of "self learning", ( since the late 90's ) I never stop being amazed at how often folks over complicate code examples - and how often the code example never actually works.

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

Code snippets with member pointing error and better implementations is really cool, it could be just posted on general programming thou, a sub-forum on general programming perhaps? Or a sub-forum for each major tech forum (snippets for networking, directX, etc..)

I think it could work cause its really ego intensive, ppl will love to show how better they can do, and its beneficial for everyone. I always want to just post something just to ask if "is that the best way to do this", but feel is just too personal and abusive post.

For example, I just implemented rounding a number up to the next multiple of a number

int RoundUp( number, multiple ); I found quite a lot of implementations for this in stack overflow, not sure whats the best., mine ended like this:


	inline uint32_t RoundUpToNextMultiple( uint32_t n_p, uint32_t multiple_p ){

		return n_p + multiple_p - 1 - (n_p - 1) % multiple_p;
	}
	
	inline int32_t RoundUpToNextMultiple( int32_t n_p, int32_t multiple_p ){

		if( n_p < 0 ){
			// multiple_p = -multiple_p; rounds in the left direction (rounds down)

			return - ((int)(RoundUpToNextMultiple( (uint32_t)-n_p, (uint32_t)multiple_p )) - multiple_p );
		}

		return RoundUpToNextMultiple( (uint32_t)n_p, (uint32_t)multiple_p );
	}

The negative checks makes the round goes towards zero, so it always rounds to the right(->) direction

RoundUpToNextMultiple( -18, 16 ); // 16

RoundUpToNextMultiple( -2, 16 ); // 0

RoundUpToNextMultiple( 0, 16 ); // 0

RoundUpToNextMultiple( 2, 16 ); // 16

RoundUpToNextMultiple( 18, 16 ); // 32

This topic is closed to new replies.

Advertisement