Thread: 3D arrays

Page 1 of 3 123 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    44
    Rep Power
    15

    3D arrays


    Hi, can anybody help me out with the code for declaring and creating an 10x10x10 array of strings, with each string initialised to "hello"? To delcare and create the array is

    String [][][]myArray = new String[10][10][10];

    but im stuck on how to initialised each string?? I know how to do it for a smaller array but as a 10x10x10 is quite large the method to initialised each one separate would be long? would i have use a loop or sumthing to initialised each row??

    Also does anybody know how class ArrayList can be implemented using an array?? Any help would be grately appreciated. Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Nov 2004
    Location
    Washington DC
    Posts
    2,755
    Rep Power
    1576
    Why do you need a 3d array? THere are very very few problems that can't be done with out using a 3d array. (a HW assignment is the only time i've used a 3d array )

    To initlize them all you'll have to have 3 nested for loops

    for() //go through the first one
    {
    for() //go through the second one
    {
    for() //go through the thrid one
    {
    }
    }
    }

    as for an array of arraylists......

    ArrayList [] foo = new ArrayList[10] will get you an array with 10 arraylists

    Comments on this post

    • StevenC agrees : Yep, I've never needed a 3d array in RL either.
  4. #3
  5. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    Chicago, IL
    Posts
    10,131
    Rep Power
    5058
    THere are very very few problems that can't be done with out using a 3d array.
    Agreed (couldn't rep++).

    Judging by the fact you're using String objects in this array, you should consider encapsulating that information in a single object and simply storing the objects in a single Collection.

    Object-Oriented Programming Concepts
    Object-oriented language basics
    Don't Fear the OOP

    Java Collections Framework
    Last edited by Yawmark; May 5th, 2005 at 11:01 AM. Reason: corrected advice...
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    254
    Rep Power
    18
    Originally Posted by Yawmark
    Agreed (couldn't rep++).

    Judging by the fact you're using String objects in this array, you should consider encapsulating that information in a single object (with three String fields) and simply storing the objects in a single Collection.

    Object-Oriented Programming Concepts
    Object-oriented language basics
    Don't Fear the OOP

    Java Collections Framework
    How about something like this:
    Code:
    public class array3d
    {
    	
    	public class array2d
    	{
    		public String data;
    		public array1d[] array1;
    		public array2d(int array1dSize)
    		{
    			array1=new array1d[array1dSize];
    		}
    	}
    	public class array1d
    	{
    		public String data;
    		
    	}
    	public array2d[] array2;
    	
    	public array3d(int array2dSize)
    	{
    		array2=new array2d[array2dSize];
    	}
    
    }
    ? Need to add some methods for array functionality, but atleast it compiles
    Thank you for any help.
  8. #5
  9. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    Chicago, IL
    Posts
    10,131
    Rep Power
    5058
    How about something like this:
    That would be an excellent example of what not to do.

    Better to design an object that encapsulates (or captures) the information about itself and its spatial (or whatever) relationship to another entity. What is the perceived need of this 3D array? What are the elemental positions supposed to signify? What is the purpose of the data? These questions (among others) should be considered when designing the class.
    Last edited by Yawmark; May 5th, 2005 at 11:04 AM.
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    254
    Rep Power
    18
    Originally Posted by Yawmark
    That would be an excellent example of what not to do.
    Why?
    Thank you for any help.
  12. #7
  13. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    Chicago, IL
    Posts
    10,131
    Rep Power
    5058
    Why?
    Sorry - you caught me mid-edit.

    The design of the class you posted is wonky. You have multiple public inner instance classes. One of those classes is named "array1d", which violates standard coding conventions, has only a single public instance member (violates encapsulation/data hiding) and encapsulates no behavior (questionable OOP), and doesn't approach anything resembling an array. The rest of the design kind of goes downhill from there.

    No offense intended; just answering the question. I recommend reading some of those links I provided earlier. Also, check out the following resources for more information on good design practices:

    The Craftsman Series
    Java Design: Building Better Apps and Applets (2nd Edition)
    Effective Java, by Joshua Bloch
    Last edited by Yawmark; May 5th, 2005 at 11:11 AM.
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    254
    Rep Power
    18
    Originally Posted by Yawmark
    Sorry - you caught me mid-edit.

    The design of the class you posted is wonky. You have multiple public inner instance classes. One of those classes is named "array1d", which violates standard coding conventions, has only a single public instance member (violates encapsulation/data hiding) and encapsulates no behavior (questionable OOP), and doesn't approach anything resembling an array. The rest of the design kind of goes downhill from there.

    No offense intended; just answering the question. I recommend reading some of those links I provided earlier. Also, check out the following resources for more information on good design practices:

    The Craftsman Series
    Java Design: Building Better Apps and Applets (2nd Edition)
    Effective Java, by Joshua Bloch
    Uhm, it was ment as a concept not a copy paste code...
    Thank you for any help.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Nov 2004
    Location
    Washington DC
    Posts
    2,755
    Rep Power
    1576
    "Why?"

    Basically it over complicates things
    It might make sense to you, but it's not so clear to others that will read your code, heck it might not be clear to you 2 months from now! I run into that a lot

    I still contend that he doesn't need to use 3d arrays at all.
  18. #10
  19. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    Chicago, IL
    Posts
    10,131
    Rep Power
    5058
    Uhm, it was ment as a concept not a copy paste code...
    Same comments apply. It's a fragile design, violates encapsulation, has no clear purpose, and is overly complicated, even as a concept.

    I still contend that he doesn't need to use 3d arrays at all.
    Agreed.
    Last edited by Yawmark; May 5th, 2005 at 11:22 AM.
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  20. #11
  21. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    254
    Rep Power
    18
    Originally Posted by Yawmark
    Same comments apply. It's a fragile design, violates encapsulation, has no clear purpose, and is overly complicated, even as a concept.

    Agreed.
    lol I should have fully read the original question. I thought he said that java had only 2d arrays. Silly me. String woot[][][][]=new String[2][3][4][5]; wow.
    Thank you for any help.
  22. #12
  23. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    Chicago, IL
    Posts
    10,131
    Rep Power
    5058
    String woot[][][][]=new String[2][3][4][5];
    Ouch, my eyes...

    Just to reiterate, multidimensional arrays are rarely (if ever) needed in Java. Someone might come up with a good example of when one is *necessary*, but I guarantee it will be the exception rather than the rule.
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  24. #13
  25. PHP & Java Error Master
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    My Computer
    Posts
    1,219
    Rep Power
    0
    So then what are your opinions on, and what is a better solution to a 2d array?
    I've found those very useful in the past.
    When I've had an object that has to have multiple columns of data that needs storing, I use 2d arrays to store it.
    2d arrays are like DB tables, and thus when storing certain data from tables 2d arrays are very useful.

    Comments on this post

    • tfecw agrees : Good question!
  26. #14
  27. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    254
    Rep Power
    18
    Originally Posted by StevenC
    So then what are your opinions on, and what is a better solution to a 2d array?
    I've found those very useful in the past.
    When I've had an object that has to have multiple columns of data that needs storing, I use 2d arrays to store it.
    2d arrays are like DB tables, and thus when storing certain data from tables 2d arrays are very useful.
    Several scientists were all posed the following question: "What is 2 * 2 ?"

    The engineer whips out his slide rule (so it's old) and shuffles it back and forth, and finally announces "3.99".

    The physicist consults his technical references, sets up the problem on his computer, and announces "it lies between 3.98 and 4.02".

    The mathematician cogitates for a while, then announces: "I don't know what the answer is, but I can tell you, an answer exists!".

    Philosopher smiles: "But what do you mean by 2 * 2 ?"

    Logician replies: "Please define 2 * 2 more precisely."

    The sociologist: "I don't know, but is was nice talking about it".

    Behavioral Ecologist: "A polygamous mating system".

    Medical Student: "4"
    All others looking astonished: "How did you know ??"
    Medical Student: "I memorized it."
    Thank you for any help.
  28. #15
  29. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    Chicago, IL
    Posts
    10,131
    Rep Power
    5058
    So then what are your opinions on
    Practice and experience.

    and what is a better solution to a 2d array?
    Depends on the PD. As previously stated, though, the most appropriate solution is often to use a well-typed Collection of objects.

    I've found those very useful in the past.
    I'm not suggesting they're not useful in certain circumstances. However, with OOP, there are often better ways of handling the problem than an old procedural, low-level array.

    When I've had an object that has to have multiple columns of data that needs storing, I use 2d arrays to store it.
    Like what?

    2d arrays are like DB tables, and thus when storing certain data from tables 2d arrays are very useful.
    Certain data, perhaps. However, I'd wager that in most cases, you'd be better served by encapsulating the "row" data as an object and storing your objects in a Collection, especially in the case of something like database rows.
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
Page 1 of 3 123 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo