Results 1 to 15 of 15
- 06-28-2011, 10:23 PM #1
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
- 06-28-2011, 10:57 PM #2
That bit of code there is not valid Java. What you want is a two-dimensional array.
With that code, access the values like this:Java Code:Object[][] foo = new Object[] { new Object[] {"Boston", new Integer(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.Java Code:System.out.println((String) foo[0][0]); // ouputs Boston System.out.println((Integer) foo[0][1]); // ouputs 7
- 06-28-2011, 11:13 PM #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-28-2011, 11:14 PM #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-28-2011 at 11:20 PM.
- 06-28-2011, 11:19 PM #5
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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-28-2011, 11:42 PM #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-28-2011, 11:57 PM #7
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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:
Note that you'll only be able to do this if the class you're accessing the code from has access to the fields.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-28-2011 at 11:59 PM.
- 06-29-2011, 12: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, 01:37 AM #9
- 06-29-2011, 05:40 AM #10
- 06-29-2011, 07:56 AM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-29-2011, 08:11 AM #12
I'd pay money to see that!
- 06-29-2011, 10:40 AM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
- 06-29-2011, 02:56 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
- 06-29-2011, 03:49 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
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, 07: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, 12:17 PM -
How to create a instance for logger?
By nirasiva in forum New To JavaReplies: 3Last Post: 06-04-2011, 08:43 AM -
[HELP] cannot create instance of jdialog???
By clydedoris in forum New To JavaReplies: 0Last Post: 07-21-2010, 09:02 AM -
Create different instance of a tablemodel
By Bill in forum AWT / SwingReplies: 6Last Post: 03-27-2008, 03:49 PM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks