Java Programming Question - I hate arrays

jonthedit

Well-Known Member
OP
Member
Joined
May 30, 2011
Messages
1,682
Trophies
0
XP
1,009
Country
Bangladesh
Hey guys! I hate arrays... a LOT.
To the point where I don't use them.
Why? Because I fail to understand how to use arrays and why to use them effectively.
I've come into a situation in game development where I feel I NEED TO USE AN ARRAY.
So, now is the time!

Here is my situation:
I have a "player" class, where players inherit the images to be displayed.
I also have a "main" class, where the players are initialized as variables.
So somewhere in my code I have:
player p1 = new player()
player p2 = new player()

Lets say I want to create a boundary obstacle.
My p1 is created through paintComponent, which I initialize a new instance of the "player" graphic.
Graphics2D Player1GFX = playergfx.create();
When the new Graphics2D variable, Player1GFX, is drawn with the DrawGraphics() method (via paintComponent) I specify the x and y variables, as well as the "p1" class we created to grab the image.

Because I am using paintComponent, I have to use "ClipBounds" to determine when intersection occurs.

Question:
How do I properly create a new "boundary" class that will have the SAME image, but all act as separate pieces that I can call upon individually? I want to specify that all 'player' variables that inherit from player() class will have a specific reaction to colliding with a "boundary" brick. I feel I need to make an array of "bricks" to achieve this.
 
  • Like
Reactions: KingBlank

Snailface

My frothing demand for 3ds homebrew is increasing
Member
Joined
Sep 20, 2010
Messages
4,324
Trophies
2
Age
40
Location
Engine Room with Cyan, watching him learn.
XP
2,255
I'm amazed but not surprised to see Java make simple things complicated through needless abstractions. Arrays should be the simplest data structures to understand given the fact they mimic the way memory is arranged. Java hides memory from the programmer so I guess this is to be expected.
 

jonthedit

Well-Known Member
OP
Member
Joined
May 30, 2011
Messages
1,682
Trophies
0
XP
1,009
Country
Bangladesh
Haha, Sorry I cant help you, because I feel the same way about arrays :)
:( Arrays do suck :P


I'm amazed but not surprised to see Java make simple things complicated through needless abstractions. Arrays should be the simplest data structures to understand given the fact they mimic the way memory is arranged. Java hides memory from the programmer so I guess this is to be expected.

Wonderful, I know.
I heard C++ is not much better :|

Anyone?
 

ilman

Gbatemp's Official Noise Eraser
Member
Joined
Jul 25, 2010
Messages
1,128
Trophies
0
Age
25
Location
Shibuya
XP
570
Country
Wonderful, I know.
I heard C++ is not much better :|

Anyone?

Incorrect, C++ handles arrays a lot easier to me at least.

And about the bounding boxes...well.
The only way I can think of it is that you make your function for checking the boundaries with the players (I persume you have one) and then make another function, which checks the boundaries of all the blocks you want with the player.

So it should end up something like this:
Code:
//this may be kinda C++ish but I don't know much about Java, so :P
bool checkOneBound (plr player, bound boundary)
{
//check your stuff
}
int checkMultiBound (plr player, bound boundary[], int numberOfBounds) //this is C++ syntax, if Java doesn't allow for passing of arrays to a function, just put the array in a class
{
    for (int i=0;i<numberOfBounds;i++)
        if (checkOneBound(player, boundary[i])) return i;//or 1 or w/e you need
}

Then you use that function to check all the players with the boundries you have.
Btw, I remember that in Java the term method was used instead of function, just in case you don't know...
 
  • Like
Reactions: cearp

Oxybelis

Well-Known Member
Member
Joined
Jan 10, 2010
Messages
350
Trophies
0
XP
383
Country
Hey guys! I hate arrays... a LOT.
To the point where I don't use them.
Why? Because I fail to understand how to use arrays and why to use them effectively.
I've come into a situation in game development where I feel I NEED TO USE AN ARRAY.
So, now is the time!
That's kinda mindblowing to program without arrays. I guess Java libraries helps with this.

Can not give an advice about your question though. Looks like logic relies on drawing component.
So it should end up something like this:

Code:
bool checkOneBound (plr player, bound boundary)
{
//check your stuff
}
int checkMultiBound (plr player, bound boundary[], int numberOfBounds) //this is C++ syntax, if Java doesn't allow for passing of arrays to a function, just put the array in a class
{
    for (int i=0;i<numberOfBounds;i++)
        if (checkOneBound(player, boundary[i])) return i;//or 1 or w/e you need
}
Looks C'ish. Both C++ and Java support overloading.
In C++ it would look like this:
Code:
bool checkBounds(player plr, bound boundary)
{
//check your stuff
}
int  checkBounds(player plr, bound boundary[], size_t numberOfBounds)
{
    for ( size_t  i = 0; i<numberOfBounds; i++)
        if (checkBounds(plr , boundary[i])) return i+1;//or 1 or w/e you need
return 0;
}

And Java arrays has length. So
Code:
bool checkBounds(player plr, bound boundary)
{
//check your stuff
}
int  checkBounds(player plr, bound[]  boundary)
{
    for (int  i = 0; i< boundary.length; i++)
        if (checkBounds(plr , boundary[i])) return i+1;//or 1 or w/e you need
return 0;
}

Btw, I remember that in Java the term method was used instead of function, just in case you don't know...
They have different meaning. method is a function. member function. But in Java function ca not be declared outside object so they are all methods.
 

FAST6191

Techromancer
Editorial Team
Joined
Nov 21, 2005
Messages
36,798
Trophies
3
XP
28,321
Country
United Kingdom
Wow, my Java skills are next to nonexistent (because screw Java and the problems it has caused for the world) but I do have to join the arrays are good stuff echo.

Sitting here I am probably going to have to say programming without arrays is a bit like language without adverbs -- theoretically possible but oh so very limiting.

The reason you would want to use them is because they are a very nice way of keeping lots of related data together, acting upon it and sending it about the place. If you have ever had issues with passing variables to a function (beyond type ones and premade functions anyway) or having to do upkeep upon their return then you probably wanted to use an array. They are extremely helpful for manipulating large pools of data, which is what an awful lot of modern computing is concerned with.
 

Jamstruth

Secondary Feline Anthropomorph
Member
Joined
Apr 23, 2009
Messages
3,462
Trophies
0
Age
31
Location
North East Scotland
XP
710
Country
Arrays are confusing? My god man. Never code in C. Pointers are my hell.

I suggest using an ArrayList rather than an Array to simplify things a bit and make your scene a little more generic (an ArrayList is just an Array that resizes as you put more objects into it). It's in the Java Util library (Eclipse should just give you the import as an error fix).

It might also be a good idea to store your players in an ArrayList as well to avoid repetitious code. This is the main advantage of an array really. It means you can loop around and do the same thing to every object rather than having to invoke it individually for each object.

Here's how I'd do it.

Code:
ArrayList<Player> players; //ArrayList of Player objects
ArrayList<Boundary> bounds; //ArrayList of all bounds
 
//Let's assume those arraylists are filled.
//Now we do a for each loop to perform code for every Player in "players"
for(Player p: players)
{
  //In here "p" is the current player we are dealing with.
  //We now need an inner loop to go around every boundary and start checking things
  for(Boundary b: bounds)
  {
      //Now in here "b" is the current boundary we are dealing with and "p" the current player
      //Do whatever collision check needed here e.g.
      checkBounds(p, b);
  }
}

For each loops are handy for quickly making a piece of code loop around every entry in an ArrayList or any other object that implements the Collection abstract class. The code I've given would work for any number of Player Objects in the "players" List and any number of Bounds Objects in the "bounds" List.
 
  • Like
Reactions: KingBlank

Psionic Roshambo

Well-Known Member
Member
Joined
Aug 12, 2011
Messages
2,246
Trophies
2
Age
50
XP
3,343
Country
United States
@Annotation(param1 = "value1", param2 = "value2")
@SuppressWarnings({"ALL"})
public class Foo<T extends Bar & Abba, U> {
int[] X = new int[]{1, 3, 5, 6, 7, 87, 1213, 2};

public void foo(int x, int y) {
Runnable r = () -> {
};
Runnable r1 = this::bar;
for (int i = 0; i < x; i++) {
y += (y ^ 0x123) << 2;
}
do {
try (MyResource r1 = getResource(); MyResource r2 = null) {
if (0 < x && x < 10) {
while (x != y) {
x = f(x * 3 + 5);
}
} else {
synchronized (this) {
switch (e.getCode()) {
//...
}
}
}
} catch (MyException e) {
} finally {
int[] arr = (int[]) g(y);
x = y >= 0 ? arr[y] : -1;
Map<String, String> sMap = new HashMap<String, String>();
Bar.<String, Integer>mess(null);
}
}
while (true);
}

void bar() {
{
return;
}
}
}

class Bar {
static <U, T> U mess(T t) {
return null;
}
}

interface Abba {
}

According to my Android Studio poking around this is how to handle an array... I am still in full newb mode so it could be Aunt Martha's recipe for cookies for all I know...

Edit: Hmmm after a bit longer looking at it, I think that code is for the UI part? Weird I asked for an Array... lol

Edit 2: Maybe this could help? I will probably read it over myself as it looks interesting.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
 

jonthedit

Well-Known Member
OP
Member
Joined
May 30, 2011
Messages
1,682
Trophies
0
XP
1,009
Country
Bangladesh
@Annotation(param1 = "value1", param2 = "value2")
@SuppressWarnings({"ALL"})
public class Foo<T extends Bar & Abba, U> {
int[] X = new int[]{1, 3, 5, 6, 7, 87, 1213, 2};

public void foo(int x, int y) {
Runnable r = () -> {
};
Runnable r1 = this::bar;
for (int i = 0; i < x; i++) {
y += (y ^ 0x123) << 2;
}
do {
try (MyResource r1 = getResource(); MyResource r2 = null) {
if (0 < x && x < 10) {
while (x != y) {
x = f(x * 3 + 5);
}
} else {
synchronized (this) {
switch (e.getCode()) {
//...
}
}
}
} catch (MyException e) {
} finally {
int[] arr = (int[]) g(y);
x = y >= 0 ? arr[y] : -1;
Map<String, String> sMap = new HashMap<String, String>();
Bar.<String, Integer>mess(null);
}
}
while (true);
}

void bar() {
{
return;
}
}
}

class Bar {
static <U, T> U mess(T t) {
return null;
}
}

interface Abba {
}

According to my Android Studio poking around this is how to handle an array... I am still in full newb mode so it could be Aunt Martha's recipe for cookies for all I know...

Edit: Hmmm after a bit longer looking at it, I think that code is for the UI part? Weird I asked for an Array... lol

Edit 2: Maybe this could help? I will probably read it over myself as it looks interesting.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html


What? That is totally unrelated to what I am asking.
 

Foxi4

Endless Trash
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,838
Country
Poland
This will probably be helpful. Jamstruth what's so hard to understand about pointers? It's probably the most simple thing you could imagine, it reflects the physical representation of data. :P
Code:
int Number = 8; //Declaration of the actual variable
int *Pointer; //Declaration of the pointer
Pointer = &Number; //Storing the address of the variable Number
 
printf("Variable Number is equal %d and it is stored in %d address in memory. The number retrieved from that address equals %d.", Number, Pointer, *Pointer);

Perhaps this will help. :)
 

Jamstruth

Secondary Feline Anthropomorph
Member
Joined
Apr 23, 2009
Messages
3,462
Trophies
0
Age
31
Location
North East Scotland
XP
710
Country
This will probably be helpful. Jamstruth what's so hard to understand about pointers? It's probably the most simple thing you could imagine, it reflects the physical representation of data. :P
Code:
int Number = 8; //Declaration of the actual variable
int *Pointer; //Declaration of the pointer
Pointer = &Number; //Storing the address of the variable Number
 
printf("Variable Number is equal %d and it is stored in %d address in memory. The number retrieved from that address equals %d.", Number, Pointer, *Pointer);

Perhaps this will help. :)


I know what pointers are. Its knowing when to use them that gets confusing to me at least. Mostly its just because I've never needed them (Java has none because reasons).
 

Foxi4

Endless Trash
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,838
Country
Poland
I know what pointers are. Its knowing when to use them that gets confusing to me at least. Mostly its just because I've never needed them (Java has none because reasons).
Of course Java doesn't have pointers - everything is relegated to the virtual machine, so you're using "memory" that doesn't actually exist, it's merely allocated in "real" memory by the virtual machine. This dumbs the process of programming down, but I personally prefer writing native code, no matter how "portable" Java is. :D

The tl;dr version of when to use a pointer and when not to use a pointer is pretty east IMO. Using a pointer is like saying "the data is there", wheras using the actual variable is saying "here is the data".
 

jonthedit

Well-Known Member
OP
Member
Joined
May 30, 2011
Messages
1,682
Trophies
0
XP
1,009
Country
Bangladesh
Of course Java doesn't have pointers since everything is relegated to the virtual machine - you're using "memory" that doesn't actually exist, it's merely allocated in "real" memory by the virtual machine. This dumbs the process of programming down, but I personally prefer writing native code, no matter how "portable" Java is. :D

Well, I've said this before:
All programming languages are easy ways out of doing real work.
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • BakerMan @ BakerMan:
    fuck ubisoft, and fuck activision
    +1
  • realtimesave @ realtimesave:
    Nintendo needs to release a new console, switch is getting such shitty little games lately lol it's pathetic
  • Purple_Heart @ Purple_Heart:
    Lmao a new flashcart... The Unlock Switch... I knew it's not fake xD
    +1
  • NinStar @ NinStar:
    A new consoles won't solve that problem
  • NinStar @ NinStar:
    It will actually make it worse
  • The Real Jdbye @ The Real Jdbye:
    well actually
    a new console won't do anything right now, because the games are still in development, that's why there are few games being released
  • The Real Jdbye @ The Real Jdbye:
    it won't make the games finish any faster
  • Veho @ Veho:
    2/3rds of launch titles for the Switch 2 will just be lazy ports of Switch games anyway.
  • The Real Jdbye @ The Real Jdbye:
    probably
  • The Real Jdbye @ The Real Jdbye:
    maybe mario kart 9 will be a launch title
  • The Real Jdbye @ The Real Jdbye:
    i really want a new mario kart
  • Veho @ Veho:
    What, you mean the endless stream of DLCs doesn't count?
  • Veho @ Veho:
    Why develop a new game when you can just sell season passes forever?
  • Veho @ Veho:
    I'm still on MKDS so I'm not bothered :tpi:
  • The Real Jdbye @ The Real Jdbye:
    i like the dlc tbh, i'd like a new game more
  • ZeroT21 @ ZeroT21:
    but the current version is still selling fine at full price
  • SylverReZ @ SylverReZ:
    Hello
  • ZeroT21 @ ZeroT21:
    sup
    +1
  • SylverReZ @ SylverReZ:
    @realtimesave, You seen the Unlock Switch flashcart yet?
  • K3Nv2 @ K3Nv2:
    I'll see the 19.0 update that blocks use ability to it
    +1
  • K3Nv2 @ K3Nv2:
    Lol newegg+
    Screenshot-20240423-053504-Gmail.jpg
  • S @ salazarcosplay:
    does update 19 really block it
  • SylverReZ @ SylverReZ:
    Update 19 never came out yet. Just the 18.1.
    SylverReZ @ SylverReZ: Update 19 never came out yet. Just the 18.1.