Results 1 to 15 of 15
Like Tree3Likes
  • 1 Post By Junky
  • 1 Post By Junky
  • 1 Post By JosAH

Thread: trying to create an array with multiple values per instance

  1. #1
    aconti is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default trying to create an array with multiple values per instance

    Hello,

    wen trying to create the below

    String abc[] = {
    new String (Boston, 7);

    I get Boston cannot be resolved to a variable

    I need to give abc[0] with both Boston and number 7 values

  2. #2
    Jonah Bron's Avatar
    Jonah Bron is offline Member
    Join Date
    Sep 2010
    Location
    California
    Posts
    26
    Rep Power
    0

    Default

    That bit of code there is not valid Java. What you want is a two-dimensional array.

    Java Code:
    Object[][] foo = new Object[] {
        new Object[] {"Boston", new Integer(7)}
    };
    With that code, access the values like this:

    Java Code:
    System.out.println((String) foo[0][0]); // ouputs Boston
    System.out.println((Integer) foo[0][1]); // ouputs 7
    Notice that, because you have to type the array as Object (since you want to store Integers and Strings), you must cast them back to their original type.

  3. #3
    Jonah Bron's Avatar
    Jonah Bron is offline Member
    Join Date
    Sep 2010
    Location
    California
    Posts
    26
    Rep Power
    0

    Default

    After further consideration, a Map might be better for this. What is the integer for? Is that an ID, or some attribute of Boston?

  4. #4
    aconti is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    the number 7 is an attribute for Boston which needs to be stored also at Instance 0

    I was sure that this was done quite simply by something like this:

    String abc[] = {
    new String (Boston, 7);

    is the above completely wrong or maybe I have some small syntax error ?
    Last edited by aconti; 06-28-2011 at 11:20 PM.

  5. #5
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    You'd be better off creating a class to group your data together.

    Java Code:
    class Foo {
        String str;
        int num;
        public Foo(String str, int num) {
            this.str = str;
            this.num = num;
        }
    }
    ...
    Foo[] fooArray = new Foo[2];
    fooArray[0] = new Foo("Boston", 7);
    fooArray[1] = new Foo("London", -34);

  6. #6
    aconti is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    unfortunately still I am not getting any output from the below...


    public class Foo {

    public static void main(String[] args) {
    }
    String str;
    int num;
    public Foo(String str, int num) {
    this.str = str;
    this.num = num;
    }



    Foo[] fooArray = new Foo[2];{

    fooArray[0] = new Foo("Boston", 7);

    fooArray[1] = new Foo("London", -34);



    System.out.println(fooArray[1]);

    }

    }

  7. #7
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    First and foremost, the above generates no output because your main() method contains no code.

    Secondly, printing fooArray[1] will not produce the output you're expecting because Foo doesn't override toString(). Instead, it'll produce a string of what appears to be gibberish.

    To be able to do anything meaningful with a Foo's fields, you'll need to do one of two things:
    • Refer to the fields directly (usually considered bad practice). Example:
      Java Code:
      System.out.println(fooArray[1].str);
      Note that you'll only be able to do this if the class you're accessing the code from has access to the fields.
    • Create getter/setter methods for each field (recommended). After creating such a method, you'd be able to do something like:
      Java Code:
      System.out.println(fooArray[1].getStringValue());
    Last edited by Iron Lion; 06-28-2011 at 11:59 PM.

  8. #8
    aconti is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    Create getter/setter methods for each field (recommended).

    Were can I get help/examples of how to set these please ?

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by aconti View Post
    unfortunately still I am not getting any output from the below...
    Another classic example of people blindly copying code without understanding it and then whine when it doesn't do what they want.
    Fubarable likes this.

  10. #10
    Jonah Bron's Avatar
    Jonah Bron is offline Member
    Join Date
    Sep 2010
    Location
    California
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by aconti View Post
    Create getter/setter methods for each field (recommended).

    Were can I get help/examples of how to set these please ?
    A setter or getter is just a method that either sets a class property to a value, or returns it. Here's a setter:

    Java Code:
    public void setString(String value) {
        this.str = value;
    }
    Here's a getter:
    Java Code:
    public string getString() {
        return this.str;
    }

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Junky View Post
    Another classic example of people blindly copying code without understanding it and then whine when it doesn't do what they want.
    What's wrong with that? Isn't that how programming is done nowadays? Personally I'm very advanced: when I copy and paste some stuff from teh net and it doesn't work I throw myself to the floor, start kicking around and scream on the top of my lungs until it's fixed.

    kindest regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    I'd pay money to see that!
    Jonah Bron likes this.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Junky View Post
    I'd pay money to see that!
    Feeding times at 1:00pm; please don't stick your hands through the fence.

    kind regards,

    Jos
    Jonah Bron likes this.
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    What's wrong with that? Isn't that how programming is done nowadays? Personally I'm very advanced: when I copy and paste some stuff from teh net and it doesn't work I throw myself to the floor, start kicking around and scream on the top of my lungs until it's fixed.

    kindest regards,

    Jos
    You're one of my sons aren't you? Why aren't you in school?

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    You're one of my sons aren't you? Why aren't you in school?
    Dad! Dad! I had to stay out of school because you forgot to pay me my allowance! Gimme my loot!

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 2
    Last Post: 06-10-2011, 07:15 AM
  2. Multiple Instance of Web Application end Up Using Same JCA Resource
    By sjunejo in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 06-09-2011, 12:17 PM
  3. How to create a instance for logger?
    By nirasiva in forum New To Java
    Replies: 3
    Last Post: 06-04-2011, 08:43 AM
  4. [HELP] cannot create instance of jdialog???
    By clydedoris in forum New To Java
    Replies: 0
    Last Post: 07-21-2010, 09:02 AM
  5. Create different instance of a tablemodel
    By Bill in forum AWT / Swing
    Replies: 6
    Last Post: 03-27-2008, 03:49 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
  •