Results 1 to 2 of 2
- 04-21-2011, 12:04 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 1
- Rep Power
- 0
How do I create a String[2][20], with only 10 items
I am working on a java class, and have been asked to create an inventory object of String[2][20] and fill it with only 10 things.
The only way I can think to do this, is as follows, but, is there a better way?
private String[][] inventory = {
{"flashlight","0"},
{"knife","0"},
{"Swiss army knife", "0"},
{"camera", "0"},
{"notebook", "0"},
{"lighter", "0"},
{"lunchbox", "0"},
{"jacket", "0"},
{"cell phone", "0"},
{"mp3 player", "0"},
{"",""},
{"",""},
{"",""},
{"",""},
{"",""},
{"",""},
{"",""},
{"",""},
{"",""},
{"",""}
};
- 04-21-2011, 12:16 AM #2
Shouldn't that be [20][2]?
Do you have an Inventory class? I'd do something like this
Alternatively you could have the method return boolean to reflect if the item was added or not.Java Code:class Inventory { private final int MAX = 20; private String[][] inventory = new String[MAX][2]; private int items = 0; public void addItem(String description, String quantity) { if(items < MAX) { inventory[items][0] = description; inventory[items][1] = quantity; items++; } else { System.out.println("Inventory is full"); } } }
Similar Threads
-
Best way to create a group of String,Double?
By ozzyman in forum New To JavaReplies: 3Last Post: 04-19-2011, 11:22 AM -
How to create dynamic string object???
By Stephen Douglas in forum New To JavaReplies: 8Last Post: 04-12-2010, 02:35 AM -
create object with name from string
By skandalouz in forum New To JavaReplies: 7Last Post: 12-05-2009, 11:40 PM -
Problem extracting items from a string with separators
By PepsiColaMola in forum New To JavaReplies: 3Last Post: 04-24-2009, 08:30 PM -
create a formula from a string
By lemon3 in forum New To JavaReplies: 4Last Post: 07-18-2008, 09:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks