setting up a multi dimensional array of objects

Hey ya'll. How would i setup a multi dimensional array with this structure?

TypeSave(Title, material(name, quantity))

TypeSave[1](Lego Shop, material[1](Lego Blocks, 100))

material[2](Roof, 1))

TypeSave[2](Lego Car, material[1](Door, 2))

material[2](Gravy, 3, ounces))

TypeSave will be saved as a serialised object to a file so i can load everything within it and keep the structures integrity.

I'll have an add button that will add the name and quantity to the next empty position of "material" array. This will then be listed in a list box and allow me to save all the materials with a title to the TypeSave list in the next empty position.

I'm getting mightily confused how to set up the classes and the array structure :(

Any help would be greatly appreciated!

[830 byte] By [KingScootya] at [2007-11-26 17:12:55]
# 1

TypeSave [] types = new TypeSave[MAX_NUMBER_OF_TYPES];

types[0] = new TypeSave(....);

types[1] = new TypeSave(...);

types[2] = new TypeSave(...);

Or, better yet, use a List<TypeSave>.

%

duffymoa at 2007-7-8 23:40:48 > top of Java-index,Java Essentials,New To Java...
# 2

TypeSave [] types = new TypeSave[MAX_NUMBER_OF_TYPES];

types[0] = new TypeSave(Title, Material[0](name,quantity));

types[0] = new TypeSave(Title, Material[1](name,quantity));

types[1] = new TypeSave(Title, Material[0](name,quantity));

types[2] = new TypeSave(...);

so something like this?

> Or, better yet, use a List<TypeSave>.

Care to explain? :)

KingScootya at 2007-7-8 23:40:48 > top of Java-index,Java Essentials,New To Java...
# 3
Can anyone help? I'm still a little confused.
KingScootya at 2007-7-8 23:40:48 > top of Java-index,Java Essentials,New To Java...
# 4
> so something like this? ...No. That's not legal. Can you try to explain what you are doing more clearly?
CaptainMorgan08a at 2007-7-8 23:40:48 > top of Java-index,Java Essentials,New To Java...
# 5

Scotty,

If this is school/college/uni assignment, or an excersise out of a book then please just type in question... one suspects that your interpretation of the question suffers a fair dgree of misinterpretation... :-)

If this is for the real world, then give up on using serialization for persistence and just hook into a DB.... [url=http://hsqldb.org/]HSQL-DB[/url] (a pure java in memory relational database) comes with java these days (starting with 1.4, I think)

Keith.

corlettka at 2007-7-8 23:40:48 > top of Java-index,Java Essentials,New To Java...
# 6

Check the below code. I think this would be helpful. If not let me know your requirements bit more:

import java.util.Hashtable;

import java.util.ArrayList;

import java.io.Serializable;

public class TypeSaveUsage{

public static void main(String args[]){

TypeSave t = new TypeSave();

t.addItem("Lego Shop", new Material("Lego Blocks","10"));

t.addItem("Lego Shop", new Material("Roof","5"));

t.addItem("Lego Car", new Material("Thinking in Java","10"));

t.addItem("Book", new Material("Thinking in Java","10"));

Hashtable items = t.getItems();

System.out.println("Lego Shop :" + t.getItem("Lego Shop"));

System.out.println("Lego Car :" + t.getItem("Lego Car"));

System.out.println("Book:" + t.getItem("Book"));

}

}

class Material implements Serializable {

String name = null;

String quantity = null;

//To restrict default constructor

private Material() {

}

public Material(String name, String quantity) {

this.name = name;

this.quantity = quantity;

}

public String getName() {

return this.name;

}

public String getQuantity() {

return this.quantity;

}

public String toString() {

return "Name : " + this.name + "Quantity : " + this.quantity;

}

}

class TypeSave implements Serializable {

private Hashtable items = null;

public TypeSave() {

items = new Hashtable();

}

public void addItem(String title,Material material) {

ArrayList materials = (ArrayList) items.get(title);

if(materials == null) {

materials = new ArrayList();

items.put(title,materials);

}

materials.add(material);

}

public Hashtable getItems() {

return this.items;

}

public ArrayList getItem(String title) {

return (ArrayList)this.items.get(title);

}

}

I_LOVE_JAVAa at 2007-7-8 23:40:48 > top of Java-index,Java Essentials,New To Java...
# 7

Thanks for the code sample :)

Basically, what i wanted to do was enter a materials name and its quantity into the relevant textboxes and click a button to add them to an array called material. At the moment when i click add it stores the material and its quantity into a listbox so i can view all the materials. I then have a text box for job title and a save job button.

I'd like to save the title and the materials into an array called job which would be saved to a file. I'll later have a display jobs tab for viewing all the saved jobs and materials required for each job.

Im just having trouble figuring out the array structure to allow for this.

KingScootya at 2007-7-8 23:40:48 > top of Java-index,Java Essentials,New To Java...
# 8

private TypeSave t = new TypeSave();

private void btnSaveItemActionPerformed(java.awt.event.ActionEvent evt) {

t.addItem(txtItemTitle.getText(), new Material(txtMaterialName.getText(),txtQuantity.getText()));

t.saveToFile();

}

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new MainForm().setVisible(true);

}

});

}

class Material implements Serializable {

String name = null;

String quantity = null;

//To restrict default constructor

private Material() {

}

public Material(String name, String quantity) {

this.name = name;

this.quantity = quantity;

}

public String getName() {

return this.name;

}

public String getQuantity() {

return this.quantity;

}

public String toString() {

return "Name : " + this.name + "Quantity : " + this.quantity;

}

}

class TypeSave implements Serializable {

private Hashtable items = null;

public TypeSave() {

items = new Hashtable();

}

public void addItem(String title,Material material) {

ArrayList materials = (ArrayList) items.get(title);

if(materials == null) {

materials = new ArrayList();

items.put(title,materials);

}

materials.add(material);

}

public Hashtable getItems() {

return this.items;

}

public ArrayList getItem(String title) {

return (ArrayList)this.items.get(title);

}

void saveToFile() {

ObjectOutputStream oos = null;

try

{

oos = new ObjectOutputStream(new FileOutputStream("item.ser"));

}

catch (IOException i)

{

System.out.println( "Error opening file");

}

try

{

oos.writeObject(items);

}

catch (IOException o)

{

System.out.println("Error writing file");

}

try

{

if(oos != null)

oos.close();

}

catch (IOException x)

{

System.out.println("Error closing file");

}

}

}

My save class was working, but i tried to merge it to test this code structure and now it's failing with excepion:

"Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException"

any ideas?

KingScootya at 2007-7-8 23:40:48 > top of Java-index,Java Essentials,New To Java...
# 9
Still having problems writing to file :(
KingScootya at 2007-7-8 23:40:48 > top of Java-index,Java Essentials,New To Java...