Results 1 to 11 of 11
  1. #1
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default Healty structure in java

    I'm trying to set up a simple program to handle a couple of objects. Up to this point l just used brute force while writing java. I did a file handling the objects like this.

    Java Code:
    public class SimObjects
    {
      public static void main(String[] args)
      {
          1. if (no objects){//Create object}
          2. else {//start iterate to let the objects act. }
      }
    I quite new to java and I like to get some advice on how to plane this better. I tried to put the creation of objects in a constructor. I take it java only use constructors once for every class, or? Then iterate in a main method. Any pros or cons? I'd be thrilled if someone showed me a proper way of doing this.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    You generally don't want to do anything in the main method other than get your program running. I suggest a structure like:
    Java Code:
    public class Driver{
        public static void main(String[] args){
            new Driver();
        }
        
        ArrayList<SimObjects> list;
    
        public Driver(){
            if(list == null){
                list = new ArrayList<SimObject>();
                for(int i=0; i<1000; i++){
                    list.add(new SimObject());
                }
            } else{
                for(SimObject so : list){
                    //do something with so
                }
            }
        }
    }
    I take it java only use constructors once for every class
    Correct, hence the name 'Constructor' - only used in construction.

    Good Luck! :D
    Last edited by quad64bit; 02-08-2011 at 04:25 PM.

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    P.S. Considering the behavior you want though, the above code is a little useless since its in the constructor. Please see my modified code which makes more sense:
    Java Code:
    public class Driver{
        public static void main(String[] args){
            new Driver();
        }
        
        public Driver(){
            //FYI, an empty constructor like this is not required. When you don't supply a
            //constructor, the java compiler creates one automatically
        }
    
        ArrayList<SimObjects> list;
        
        //First time print is called, it will create the objects. Subsequent calls will simply 
        //print the existing list.
        public void print(){
            if(list == null){
                list = new ArrayList<SimObject>();
                for(int i=0; i<1000; i++){
                    list.add(new SimObject());
                }
            } else{
                for(SimObject so : list){
                    System.out.println(so.toString());
                }
            }
        }
    }
    Last edited by quad64bit; 02-08-2011 at 04:25 PM.

  4. #4
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default

    Thanks!

    Things are getting slightly, clearer.

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    I'm sorry, I made a typo in my code above.
    list = new ArrayList<SimObject>;
    should be
    list = new ArrayList<SimObject>();
    I forgot the parens! I'll update my previous post to reflect this.

  6. #6
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default

    It's all right. I really liked the ArrayList you showed and what you were able to do with it. Could you describe the difference between Array and and ArrayList. Or maybe post a link? Pretty please!

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    Yeah, no problem.
    An Array is a language feature and is used in this way:
    Java Code:
    int[] arrayOfInts = new int[10];
    You can make an array of any type simply by declaring a variable with its type suffixed by []'s. The 10 is the size of the array, and it is fixed, it cannot be changed.

    ArrayList on the other hand is actually a class written with java. Its what is known as a dynamic array or dynamic vector. This means it can start small and grow as large as you want. Its based on arrays behind the scenes, but as it runs low on space, it automatically creates a larger array and copies all data into it, replacing the original.

    Arrays are great to use when you have a fixed, known number of elements. They are light weight, and very simple to work with. ArrayLists are great when the number of elements is unknown or frequently changing. ArrayList also has many convenience methods built in - insertion, deletion, contains, iteration, etc... You can also use the java Collections class to do things like sorting: Collections.sort(someArrayList);

    Does that help?

  8. #8
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by quad64bit View Post
    Does that help?
    More than you'll ever know. Thanks a million!

  9. #9
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    Sure any time! Please feel free to ask more questions like that - they are good questions and beneficial to many.

  10. #10
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default

    Hi again!

    I did an exercise some days ago, dealing with arrays. Now I'd like to try using ArrayList. I have a class called Turtle and the program is supposed to get these turtles to mess about in a 2D world.

    When I did the exercise I used placed my Objects in an over sized array. But now knowing what you said about ArrayList I like to try a more dynamic approach. I start out like this to create an ArrayList containing Turtles.
    Java Code:
        Random randGenerator = new Random();
        
        
        //Create world
        int worldSize = randGenerator.nextInt(200) + 600; 
        World w = new World(worldSize,worldSize); 
        
    //Create ArrayList containing Turtle(s)
     ArrayList<Turtle> turtles = new ArrayList<Turtle>();
        turtles.add(new Turtle(40, 60, w)); //looped in the program
        turtles.add(new Turtle(50, 50, w));
        turtles.add(new Turtle(60, 40, w));
    
        System.out.println(turtles);
    Your way seems a bit out of reach at the moment. Said by a newbie. This seems to get the work done. Well, obviously I populate the ArrayList in a loop.

    I'll let you know if I mess up.

  11. #11
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    Your code looks ok to me! You can get the turtles back out with turtles.get(3) where 3 is the 4th turtle in the list. You can also do faster iteration using a for each loop:
    Java Code:
    for(Turtle turtle: turtles){
        //do something with turtle
        System.out.println(turtle);
    }
    Remember! turtle in the above example is an OBJECT. This means if you just print it like that, you only get it's memory address. To print something other than the memory address, you need to add a toString method to your Turtle class:
    Java Code:
    public class Turtle{
        //...
        
        public String toString(){
            return "X: " + x +"\nY: "+ y +"\nName: "+name;
        }
    }
    In my example, I am printing the x, y and name fields of the turtle (or whatever fields you want!). If Turtle has this method (it must be called toString() and have a String return type) then doing System.out.println(someTurtle); will print whatever you put in the toString() method.

Similar Threads

  1. programming structure in java
    By kmm1977 in forum New To Java
    Replies: 7
    Last Post: 01-20-2011, 08:57 AM
  2. java structure
    By PBHATT in forum New To Java
    Replies: 2
    Last Post: 08-31-2010, 08:09 PM
  3. Tree structure using JAVA
    By trill in forum Advanced Java
    Replies: 2
    Last Post: 05-27-2010, 11:02 AM
  4. Passing a structure from Java to C using JNI
    By vipin27 in forum New To Java
    Replies: 0
    Last Post: 02-23-2010, 06:22 AM
  5. Java Array of Structure
    By PAffiliates in forum New To Java
    Replies: 1
    Last Post: 01-28-2008, 06:08 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
  •