Results 1 to 11 of 11
Like Tree2Likes
  • 1 Post By Norm
  • 1 Post By kneitzel

Thread: In java can you do arrays with variables?

  1. #1
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    176
    Rep Power
    1

    Default In java can you do arrays with variables?

    So like, in lua programming language you can do things like,
    Array = {1, 2, 3, abc = 5, efg = {123, 456, 789, hij = {"tests","works!"}}, hij = true}
    Array[1] = 5
    Array[3] = true

    Can you do atleast something like this in java or?
    I would like to do this because if let's say I was making a game, I could define what tiles are passable and which are not and then their location or something, so like this:

    //p (passable) stands for if possible to walk on
    //c stands for tile image

    t = ["grass.png","water.png","chest.png"]

    Tiles = [
    [p = false, c = t[1], x = 3, y = -2 ],
    [p = true, c = t[0], x = 4, y = 3 ],
    [p = false, c = t[2], x = 7, y = 0 ],
    [p = true, c = t[0], x = 1, y = 4 ],
    [p = true, c = t[0], x = 9, y = 5 ],
    [p = false, c = t[2], x = 0, y = 6 ]
    ]

    String c;
    int x;
    int y;
    for (int i = 0; i > Tiles.length; i++) {
    x = Tiles[i[x]];
    y = Tiles[i[y]];
    c = Tiles[i[c]];
    drawImage(c,x,y);
    }

    if (key = "up") {
    //missing code, tests wether the tile co-ords above there's an unpassable tile {
    collide = true;
    }
    }

    Or something...
    Last edited by augustas656; 04-07-2014 at 04:31 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: In java can you do arrays with variables?

    For starters see the tutorial: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Arrays contain one type of data. For example: String or int or boolean. The examples you've posted isn't allowed.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: In java can you do arrays with variables?

    Your question is hard to understand but I'll try to see if we are on the same page...

    First thing to know is that Java is type-safe, meaning :
    int[] a; can only store ints and other types that fit into int's memory, not boolean, not strings...

    int[] a = { "Kagiso", true} // this won't even go past the compiler

    BUT if you wanted to know if it's possible to do something like this:

    int num = 6;
    int[] nums = {10, num};
    then Yes it's possible

    See the link that was provided by Norm
    Last edited by Kagiso; 04-07-2014 at 04:33 PM.

  4. #4
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    176
    Rep Power
    1

    Default Re: In java can you do arrays with variables?

    Just edited my post, how could I do that?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: In java can you do arrays with variables?

    Have you read the tutorial on arrays?

    BTW:
    []s are used to wrap array indexes, {}s are used to wrap blocks of code
    = is the assignment operator, not the equality testing operator: ==

    Are you the same person that said this:
    Ermm, I know java, I've learnt it, all basic terminology,
    in this thread: Java 2.D Game, place image on to JPanel or JFrame
    Last edited by Norm; 04-07-2014 at 04:46 PM.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    176
    Rep Power
    1

    Default Re: In java can you do arrays with variables?

    Quote Originally Posted by Norm View Post
    Have you read the tutorial on arrays?

    BTW:
    []s are used to wrap array indexes, {}s are used to wrap blocks of code
    = is the assignment operator, not the equality testing operator: ==

    Are you the same person that said this:

    in this thread: Java 2.D Game, place image on to JPanel or JFrame
    Yes I am the same person, and my example above with the arrays is very very crude, if I go back there now I can see many faults myself, and that example was written in mostly lua programming, or in-between. I had the {}, because that's lua programming language's array symbols, and I have watched youtube tutorials of arrays and I know you can do int[], String[] etc, but I was just wondering if it was possible to do multi-type arrays. So that's why I asked here. Clear?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: In java can you do arrays with variables?

    it was possible to do multi-type arrays
    Not like you want.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    176
    Rep Power
    1

    Default Re: In java can you do arrays with variables?

    Thank you, but now further, I assume that there is something similar to what I meant, because you said not like I want. So, how can it work like?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: In java can you do arrays with variables?

    An array of Objects can hold any class item.
    Kagiso likes this.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: In java can you do arrays with variables?

    But however, the array of Objects will not hold premitives... they'll be wrapped

  11. #11
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: In java can you do arrays with variables?

    I would suggest to create a clean object model. If you create a class that can hold the data you want to store, then you can have an array of such instances. An array of object can store any class item but you risk that you run into runtime trouble when you read something that is of a different class than expected.

    And then your class can contain another array, too. you should also check the types that Java offers. Java comes with a lot of quite nice types e.g. lists, hashmaps, ...

    But without details about your project, it is quite hard to help with your design. Maybe a book covering object oriented design could be useful.

    With kind regards,

    Konrad
    Norm likes this.

Similar Threads

  1. See all variables and arrays?
    By Stud1 in forum Eclipse
    Replies: 11
    Last Post: 01-16-2014, 03:29 PM
  2. Creating variables/arrays while the program runs
    By AGuyWhoSkis in forum New To Java
    Replies: 3
    Last Post: 05-31-2013, 05:40 AM
  3. Replies: 2
    Last Post: 08-26-2012, 02:49 AM
  4. Replies: 4
    Last Post: 08-12-2011, 06:47 PM
  5. Using Arrays as Variables.
    By wired-in=p in forum New To Java
    Replies: 4
    Last Post: 07-24-2011, 04:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •