Results 1 to 15 of 15
- 06-28-2011, 11:23 PM #1
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
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
- 06-28-2011, 11:57 PM #2
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)} };
Java Code:System.out.println((String) foo[0][0]); // ouputs Boston System.out.println((Integer) foo[0][1]); // ouputs 7
- 06-29-2011, 12:13 AM #3
After further consideration, a Map might be better for this. What is the integer for? Is that an ID, or some attribute of Boston?
- 06-29-2011, 12:14 AM #4
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
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-29-2011 at 12:20 AM.
- 06-29-2011, 12:19 AM #5
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 2
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);
- 06-29-2011, 12:42 AM #6
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
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]);
}
}
- 06-29-2011, 12:57 AM #7
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 2
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);
- 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-29-2011 at 12:59 AM.
- 06-29-2011, 01:46 AM #8
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
Create getter/setter methods for each field (recommended).
Were can I get help/examples of how to set these please ?
- 06-29-2011, 02:37 AM #9
- 06-29-2011, 06:40 AM #10
- 06-29-2011, 08:56 AM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 10,407
- Blog Entries
- 7
- Rep Power
- 16
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,
JosMy mother never saw the irony in calling me a son-of-a-bitch.
- 06-29-2011, 09:11 AM #12
I'd pay money to see that!
- 06-29-2011, 11:40 AM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 10,407
- Blog Entries
- 7
- Rep Power
- 16
- 06-29-2011, 03:56 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 8,954
- Rep Power
- 13
- 06-29-2011, 04:49 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 10,407
- Blog Entries
- 7
- Rep Power
- 16
Similar Threads
-
How to string.indexOf for multiple strings and return first instance?
By Arrowx7 in forum New To JavaReplies: 2Last Post: 06-10-2011, 08:15 AM -
Multiple Instance of Web Application end Up Using Same JCA Resource
By sjunejo in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 06-09-2011, 01:17 PM -
How to create a instance for logger?
By nirasiva in forum New To JavaReplies: 3Last Post: 06-04-2011, 09:43 AM -
[HELP] cannot create instance of jdialog???
By clydedoris in forum New To JavaReplies: 0Last Post: 07-21-2010, 10:02 AM -
Create different instance of a tablemodel
By Bill in forum AWT / SwingReplies: 6Last Post: 03-27-2008, 04:49 PM
Bookmarks