Page 1 of 2 12 LastLast
Results 1 to 20 of 21
Like Tree1Likes

Thread: Need help with understanding arrays

  1. #1
    togie is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Need help with understanding arrays

    Hey guys. I have the basics of arrays and multi dimensional arrays

    but im having trouble trying to figure out how to store an array into a multi dimensional array.

    Need help with understanding arrays-descrition.png

    Any help would be greatly appreciated
    Java Code:
    public class buildMap {
    	
    	public static void buildMap(){
    		String[][] map = new String[framework.mapHeight][framework.mapWidth];
    		
    		 for (int r=0; r<map.length; r++) {
    		     for (int c=0; c<map[r].length; c++) {
    		    	 map[r][c]=forestName.getForestName();
    		    	 if(framework.villageChance > framework.getPercent()){
    		    		 map[r][c] = villageName.getVillageName();
    		    	 }
    		         System.out.print(map[r][c]+"\n");
    		     }
    		 }
    		 framework.map=map;
    
    	}
    }
    As you can see for now i am just storing the name of either a forest or town in my multi dimensional array but i would like to change that to storing a description aswell as a int to determine (0 = town, 1 = forest)

    and yes i now i probably shouldent be using static stuff everywhere but its how i know to code it at the moment.
    Last edited by togie; 11-18-2013 at 02:36 PM.

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

    Default Re: Need help with understanding arrays

    how to store an array into a multi dimensional array.
    A sample:
    Java Code:
       int[][] twoDim = new int[2][];    //  define 2 dim array
       twoDim[0] = new int[3];    //  Store a 1 dim array in a 2 dim array
       System.out.println(Arrays.deepToString(twoDim)); // [[0, 0, 0], null]
    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    Last edited by Norm; 11-17-2013 at 09:51 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    togie is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: Need help with understanding arrays

    Sorry i will remember for next time ... the problem i am having is that i want to store an int and a string to a single position in an array

    Java Code:
    int[][] twoDim = new int[2][];
    twoDim[0] = new int[3];
    twoDim[0] = new String[3]; // Would also like to add this
    System.out.println(Arrays.deepToString(twoDim));
    obviously this isnt possible like this but is there another way ?

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

    Default Re: Need help with understanding arrays

    want to store an int and a string to a single position in an array
    Two problems with that
    1) arrays only hold one type of data. You can NOT have int and String in the same array.
    2) a variable can only hold ONE value. You can not store two values in one variable.

    One way to have multiple values in one object is to define a class that holds all the values. Create an instance of the class with all the values you want and then store a reference to that instance in the array.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,346
    Rep Power
    6

    Default Re: Need help with understanding arrays

    Only one type of data, but that one type of data might just represent multiple values.

    Java Code:
    String[] values = new String[...];
    values[0] = "1;some text";
    values[1] = "2;some other text";
    One string, two values. Only you need a little more effort to get them out again :p

    NOTE: I'm not making any recommendations. Just extending the realm of what is possible and what is not possible.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,017
    Rep Power
    6

    Default Re: Need help with understanding arrays

    Or multiple types of data:

    Java Code:
    Object [] values = new Object[3];
    values[0] = new Integer(1);
    values[1] = new Double(2.4);
    values[2] = "Hello, World!";
    And it still takes some work to get them out as their original type.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,346
    Rep Power
    6

    Default Re: Need help with understanding arrays

    @Jim: should we be punished for showing the stuff you just shouldn't do?

    Nah its Norm's fault for making such claims as you CANNOT bla die bla die bla. That's just asking for it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: Need help with understanding arrays

    Yes, one should not use ALWAYS or NEVER because some wisea** will find an exception. Some of the above maybe useful for the OP and some will just be confusing.

    Too many bored programmers around here looking for something to do.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,017
    Rep Power
    6

    Default Re: Need help with understanding arrays

    Quote Originally Posted by Norm View Post
    because some wisea** will find an exception.
    I'm honored!! Or are you talking about the "other guy?" :)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,346
    Rep Power
    6

    Default Re: Need help with understanding arrays

    Probably both. I mean, we are wise.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    togie is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: Need help with understanding arrays

    If you guys are bored you could always make a tutorial on making a basic mud ... i just want to rip my hair out trying to figure it out.

    i was able to make a 30 by 30 map with room names and descriptions but storing data in each one of the rooms is whats getting me ( aka shop data, item data, mob data ...ect)

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

    Default Re: Need help with understanding arrays

    storing data in each one of the rooms
    Define a class: Room and it can hold as much data about a room as you want.

    If you must use an array of Strings, put each room's data in columns on one row.
    Define the column indexes:
    Java Code:
    final int ShopIdx = 0;
    final int ItemIdx = 1;
    final int MobIdx = 2;
     ...  etc for rest of columns
    
    rooms[roomIdx][ShopIdx] = "the shop data";  //  save the shop data for this room
    Then use those variables in the code instead of hardcoding the indexes as 0,1,2 etc
    Last edited by Norm; 11-18-2013 at 11:54 PM.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,017
    Rep Power
    6

    Default Re: Need help with understanding arrays

    First, are you allowed to use classes? Second, how many different types of data do you have? Is it really 30? So basically, 30 types of data with 30 entries for each data type? Is that correct?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    togie is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: Need help with understanding arrays

    x - 30 by y - 30 making 900 rooms.

    my first plan was to use a multi array but its killing my brain trying to figure out how to put say an integer and 2 strings into one position in the array (int isTown, String Name, String Desc)

    Im still new at this so this is probably above my level but i figure if you dont screw up you never learn lol

    ok think about this. Using my image at the top, how would i aproach that properly. aswell i would need to be able to add objects into it (drop a sword / pick up a sword)

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

    Default Re: Need help with understanding arrays

    Are your rooms indexed by two values: x and y
    For example the building has 30 floors and each floor has 30 rooms
    So the indexes would be named: theFloor and theRoom instead of x and y

    If each room has attributes, then add a third dimension for the attributes:
    rooms[NbrFloors][NbrRoomsOnFloor][NbrAttrbutes]

    To access a room's Shop data:
    rooms[theFloor][theRoom][ShopIdx]
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    togie is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: Need help with understanding arrays

    yes i had though about that. you make it alot clearer with Idx ... i had never thought of having indexes refering to different shops. how would i go about storing an array list to hold items and such that are also in the room though ? that would be my next road block.

    thank you for the help by the way, its much appreciated

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

    Default Re: Need help with understanding arrays

    y list to hold items and such that are also in the room though
    The things in the room are in the third dimension of the array.
    1st dim = floor
    2nd dim = room on a floor
    3rd dim = contents of a room
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    togie is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: Need help with understanding arrays

    This is just a code im trying outside my actual game ... im having trouble putting the object shop into my int array becuase it contains strings am i going about adding the room contents wrong ?


    Java Code:
    package myGame;
    
    import java.util.ArrayList;
    
    public class map {
    	final static int ShopIdx = 0;
    	final static int ItemIdx = 1;
    	final static int MobIdx = 2;
    	static int totalFloors = 5;
    	static int totalRooms = 2;
    	
    	public static class shop{
    		
    		public shop(){
    			String name = "test";
    		}
    	}
    
    	
    	public static void test(){
    		ArrayList<String>  v = new ArrayList<String>(); //Ignore
    		 v.add( "test" ); //Ignore
    		 
    		int[][][] rooms = new int[totalFloors][totalRooms][4];
    		rooms[0][0][ShopIdx] = new shop();
    	    
    	    System.out.print(rooms[0][0][ShopIdx]);
    	}
    	
    	public static void main(String[] args){
    		test();
    	}
    }
    Last edited by togie; 11-19-2013 at 05:15 AM.

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

    Default Re: Need help with understanding arrays

    having trouble putting the object shop into my int array
    That was discussed in post#4.

    You can't mix types. If you want to store Shop objects the array must be of type Shop
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    togie is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: Need help with understanding arrays

    hmm, thats where im having a problem though ... i also tryed that but i cant seem to make shop an array list... i need to add items into it... its just very confusing , like i said im quite new to java

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 02-27-2013, 10:53 AM
  2. Copying Single Arrays to 2-D Arrays
    By jmscarlet9 in forum New To Java
    Replies: 7
    Last Post: 04-03-2012, 12:17 AM
  3. Need some understanding and help!
    By Kevinius in forum New To Java
    Replies: 8
    Last Post: 05-14-2011, 06:50 AM
  4. Am I understanding this correctly?
    By phixion in forum New To Java
    Replies: 9
    Last Post: 01-27-2011, 06:13 AM
  5. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM

Posting Permissions

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