Results 1 to 2 of 2
  1. #1
    Arkazex is offline Member
    Join Date
    May 2013
    Posts
    1
    Rep Power
    0

    Default Issue with Lists

    Hello, and sorry if this isn't in the right section, it just didn't seem advanced enough for the advanced section.

    I'm trying to write a program that can write to the Valve Map Format, and I have a method that takes "VClass" objects and allows them to be nested within each other, but for my program, whenever I attempt to nest several "VClass" objects within eachother, only the last one is shown. Here's the code in question:

    VClass.java
    Java Code:
    package com.arkazex.sourcebukkit.vmf;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class VClass {
    	
    	//Here the template values for what a field should look like are defined
    	private String header = "%FIELDNAME%\n{";
    	private String lineheader = "	";
    	private String footer = "}";
    	
    	//Keep the field name for future reference
    	private String fieldname;
    	
    	//Here is where actual content for the field will be stored
    	private String content = "	";
    	
    	//Here we will store properties so they can be accessed and changed
    	Map<String,Property> properties = new HashMap<String,Property>();
    	
    	//Here we will store subclasses so they can be accessed and changed
    	Map<String,VClass> classes = new HashMap<String,VClass>();
    	
    	
    	
    	
    	//Customize the template headers for this specific class instance
    	public VClass(String fieldname) {
    		//Search the header string for the field name placeholder, and replace it with the actual field name
    		header = header.replace("%FIELDNAME%", fieldname);
    		
    		//Save the field name for future reference
    		this.fieldname = fieldname;
    	}
    	
    	//Add an existing property to the class
    	public void addProperty(Property property) {
    		//Take the given property, and put it in the properties map
    		properties.put(property.getName(), property);
    	}
    	
    	//Nests another class beneath this one
    	public void addSubclass(VClass subclass) {
    		//Take the given class, and put it into the classse map
    		classes.put(subclass.getFieldname(), subclass);
    		
    	}
    	
    	//Compiles all the template and content data into it's final format
    	public String getFinalClass() {
    		//Retrieve all the properties and subclasses as an array
    		System.out.println(properties.size());
    		System.out.println(classes.size());
    		
    		Property[] properties = this.properties.values().toArray(new Property[this.properties.size()]);
    		VClass[] classes = this.classes.values().toArray(new VClass[this.classes.size()]);
    		
    		//Add all the properties to the content field
    		for(int i = 0; i < properties.length; i++) {
    			content = content + "\n" + lineheader + properties[i].getFinalFormat();
    		}
    		
    		for(int i = 0; i < classes.length; i++) {
    			//Get the raw finalClass information from the target class
    			String raw = classes[i].getFinalClass();
    			
    			//Replace every line break with a line break followed by the templates lineheader
    			raw = raw.replace("\n", "\n" + lineheader);
    			
    			//Add the first line header to the beginning of the data
    			raw = lineheader + raw;
    			
    			//Add the edited information from the subclass to this class
    			content = content + "\n" + raw;
    		}
    		
    		return header + "\n" + content + "\n" + footer;
    	}
    	
    	public String getFieldname() {
    		return fieldname;
    	}
    }
    This is the piece of code that seems to be giving me the issues. It's where the several different side objects (All of which I have confirmed contain information) are supposidly added to a VClass object, but upon export, only the sixth (last) object is in the final piece of code.

    The issue seems to be between lines 42 and 54.

    Solid.java
    Java Code:
    package com.arkazex.sourcebukkit.brushes;
    
    import com.arkazex.sourcebukkit.vmf.Property;
    import com.arkazex.sourcebukkit.vmf.VClass;
    
    
    public class Solid {
    	//Here are the Sides that make up the brush in question
    	private Side north;
    	private Side south;
    	private Side east;
    	private Side west;
    	private Side top;
    	private Side bottom;
    	
    	//Store the id of the solid
    	private int id;
    
    	//Save the six sides generated by god knows what
    	public Solid(Side north, Side south, Side east, Side west, Side top, Side bottom) {
    		//save different sides 
    		this.north = north;
    		this.south = south;
    		this.east = east;
    		this.west = west;
    		this.top = top;
    		this.bottom = bottom;
    		
    		//Generate and save a unique ID
    		this.id = getID();
    	}
    	
    	//Turn the sides into a vclass that can be saved into the vmf document
    	public VClass getFinal() {
    		//Create a new vclass to add on to
    		VClass newclass = new VClass("solid");
    		
    		//Create a property for the ID and save it
    		newclass.addProperty(new Property("id", "" + id));
    		
    		//Add all of the subclasses to the vclass
    		VClass northclass = north.getVClass();
    		VClass southclass = south.getVClass();
    		VClass eastclass = east.getVClass();
    		VClass westclass = west.getVClass();
    		VClass topclass = top.getVClass();
    		VClass bottomclass = bottom.getVClass();
    		
    		newclass.addSubclass(northclass);
    		newclass.addSubclass(southclass);
    		newclass.addSubclass(eastclass);
    		newclass.addSubclass(westclass);
    		newclass.addSubclass(topclass);
    		newclass.addSubclass(bottomclass);
    		
    		//Return the generated class
    		return newclass;
    	}
    	
    	
    	
    	//Stores the last UID so a new one can be created.
    	private static int lastID;
    	
    	//Returns a unique ID to be used by a new solid
    	private int getID() {
    		lastID = lastID + 1;
    		return lastID;
    	}
    }
    Any help would be greatly appreciated, and If I posted this in the wrong section, I am very sorry.

    Thanks,
    - Ark

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

    Default Re: Issue with Lists

    Sounds like a referencing issue. Haven't looked at your code but are you using the same variable to reference the VClass objects and then add that same reference over and over again. Well it doesn't matter how many references you make they will all be "pointing" to the same object, the last one.

Similar Threads

  1. Help with Lists
    By Darkzombies in forum New To Java
    Replies: 2
    Last Post: 05-04-2013, 08:50 PM
  2. using lists in maps
    By Hazza in forum New To Java
    Replies: 2
    Last Post: 04-26-2013, 05:14 PM
  3. Want to know something about lists.
    By orushw in forum New To Java
    Replies: 2
    Last Post: 05-08-2012, 09:41 PM
  4. Regular Expression issue and setName() method issue
    By geforce in forum New To Java
    Replies: 2
    Last Post: 01-30-2012, 03:33 AM
  5. Help with lists
    By datreta in forum New To Java
    Replies: 6
    Last Post: 10-29-2010, 11:33 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •