Java Programming Question - I hate arrays

Discussion in 'General Off-Topic Chat' started by jonthedit, Aug 5, 2014.

  1. jonthedit
    OP

    jonthedit GBAtemp Advanced Maniac

    Member
    1,691
    438
    May 30, 2011
    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.
     
    KingBlank likes this.
  2. KingBlank

    KingBlank King of Nothing

    Member
    562
    218
    Sep 17, 2008
    New Zealand
    New Zealand
    Haha, Sorry I cant help you, because I feel the same way about arrays :)
     
  3. Snailface

    Snailface My frothing demand for 3ds homebrew is increasing

    Member
    4,324
    1,983
    Sep 20, 2010
    Engine Room with Cyan, watching him learn.
    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.
     
    Psionic Roshambo, cearp and Foxi4 like this.
  4. jonthedit
    OP

    jonthedit GBAtemp Advanced Maniac

    Member
    1,691
    438
    May 30, 2011
    Bangladesh
    :( Arrays do suck :P


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

    Anyone?
     
  5. ilman

    ilman Gbatemp's Official Noise Eraser

    Member
    1,130
    243
    Jul 25, 2010
    Shibuya
    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...
     
    cearp likes this.
  6. Oxybelis

    Oxybelis GBAtemp Fan

    Member
    349
    74
    Jan 10, 2010
    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.
    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;
    }
    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.
     
  7. FAST6191

    FAST6191 Techromancer

    pip Reporter
    23,868
    9,780
    Nov 21, 2005
    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.
     
  8. Jamstruth

    Jamstruth Secondary Feline Anthropomorph

    Member
    3,456
    185
    Apr 23, 2009
    North East Scotland
    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.
     
    KingBlank likes this.
  9. Psionic Roshambo

    Psionic Roshambo GBAtemp Advanced Maniac

    Member
    1,868
    711
    Aug 12, 2011
    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
     
  10. jonthedit
    OP

    jonthedit GBAtemp Advanced Maniac

    Member
    1,691
    438
    May 30, 2011
    Bangladesh

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

    Foxi4 On the hunt...

    pip Reporter
    23,671
    21,712
    Sep 13, 2009
    Poland
    Gaming Grotto
    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. :)
     
  12. Jamstruth

    Jamstruth Secondary Feline Anthropomorph

    Member
    3,456
    185
    Apr 23, 2009
    North East Scotland

    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).
     
  13. Foxi4

    Foxi4 On the hunt...

    pip Reporter
    23,671
    21,712
    Sep 13, 2009
    Poland
    Gaming Grotto
    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".
     
  14. jonthedit
    OP

    jonthedit GBAtemp Advanced Maniac

    Member
    1,691
    438
    May 30, 2011
    Bangladesh
    Well, I've said this before:
    All programming languages are easy ways out of doing real work.
     
  15. Foxi4

    Foxi4 On the hunt...

    pip Reporter
    23,671
    21,712
    Sep 13, 2009
    Poland
    Gaming Grotto
    Real work being? Nobody fiddles around in binary anymore, son - there's no reason to do that when you can create machine code in more convenient ways. :P
     
  16. jonthedit
    OP

    jonthedit GBAtemp Advanced Maniac

    Member
    1,691
    438
    May 30, 2011
    Bangladesh
    Tsk Tsk. Lazy Lazy :P
     
  17. Foxi4

    Foxi4 On the hunt...

    pip Reporter
    23,671
    21,712
    Sep 13, 2009
    Poland
    Gaming Grotto
    Ah, yes - the only noteworthy method of coding is carving in binary directly on a flash chip using your d*ck. B-)

    #HipsterProgramming
     
    jonthedit and Snailface like this.
  18. Snailface

    Snailface My frothing demand for 3ds homebrew is increasing

    Member
    4,324
    1,983
    Sep 20, 2010
    Engine Room with Cyan, watching him learn.
    I've heard that quote before somewhere ...

    Miss that guy. :P
     
    Foxi4 likes this.
  19. Foxi4

    Foxi4 On the hunt...

    pip Reporter
    23,671
    21,712
    Sep 13, 2009
    Poland
    Gaming Grotto
    10 points for Snailfacedor!
     
    Snailface likes this.