Failed to store values in ArrayList of class objec

2019-03-04 12:23发布

This is basically a Java code converter. It involves a GUI let user input class type, name and method. To store the values, I've created a class VirtualClass with an ArrayList<VirtualClass> classes to store variables boolean isPrivate, String className and String methodName. However, I found that nothing was stored into the ArrayList...please help me to see what's the problem

Below is the class VirtualClass

import java.util.*;

public class VirtualClass {

    private static ArrayList<VirtualClass> classes = new ArrayList<VirtualClass>();
    private boolean isPrivate;
    private String className;
    private String methodName;

    public void setVirtualClass(String name, String method, boolean isP){
        this.className = name;
        this.isPrivate = isP;
        this.methodName = method;
    }

    public void createClass(String name, String method, boolean isP){
        this.className = name;
        this.isPrivate = isP;
        this.methodName = method;
        classes.add(this);
    }

For reference, here's some relevant code from the GUI which let users create class

public class GuiAddClass extends JFrame{
    private VirtualClass stObject;
        ...

private class Handler implements ActionListener{

    public void actionPerformed(ActionEvent event){

        String cName = inputClassName.getText();
        String mName = inputMethodName.getText();
        boolean isP = true;

        if (classObject.checkName(cName) == false){

            JOptionPane.showMessageDialog(null, "Class name invalid. " +
                    "\nEntered name should not contain java keywords or equal to other existing names. " +
                    "\nPlease try again."); 

        } else if (classObject.checkName(cName) == true) {

            JOptionPane.showMessageDialog(null, "Class saved."); 
                    // this message pane has popped up
            cName = inputClassName.getText();
            mName = inputMethodName.getText();

            if (event.getSource() == publicButton) {
                isP = false;
            } else if (event.getSource() == privateButton) {
                isP = true;
            }
            stObject = new VirtualClass();
            stObject.createClass(cName, mName, isP);
        }

    }// end actionPerformed()

}// end Handler class

And here's a couple of methods from another class for display the final javaCode

public String getClassName(){
    String cName = "classname";
    String c = "c";
    for (int i=0; i<classes.size(); i++){
        c = classes.get(i).className;
    }
    cName = c;
    return cName;
}    

public String getMethodName(){
    String mName = "methodname";
    String m = "m";
    for (int i=0; i<classes.size(); i++){
    m = classes.get(i).methodName;
    }
    mName = m;
    return mName;
}

public boolean getIsPrivate(){
    boolean isP = false;
    for (int i=0; i<classes.size(); i++){
        isP = classes.get(i).isPrivate;
    }
    return isP;
}

Here's the method to generate the Java code

    public String getJavaCode(){
        String javaCode = (classObject.getPublic() + " class " + 
stObject.getClassName() + stObject.getListSize() + 
"{\n"+"\t"+"public void "+stObject.getMethodName()+"{\n"+"\t}"+"\n}");
        return javaCode;

And what would display in my programme is like this, where c should be class name, m should be method name, and 0 = classes.size()

public class c0{
    public void m{
    }
}

Can anyone help me to spot out the problem please? I just have no idea and the answers I received doesn't seem to work. Please help!

2条回答
我命由我不由天
2楼-- · 2019-03-04 12:42

From information you posted, seems strange that you initiate VirtualClass stObject into actionPerformed method. Its mean that each time you recreate your object.

Make your VirtualClass stObject to be global for example, like:

private VirtualClass stObject;

...

stObject = new VirtualClass();

private class Handler implements ActionListener{

    public void actionPerformed(ActionEvent event){

    ...

   stObject.createClass(cName, mName, isP);
查看更多
Evening l夕情丶
3楼-- · 2019-03-04 12:50

You have a couple of problems with your code. But mainly, you creational logic does not make much sense. You should not add an instance into your Static collection using a method of the instance. My advice would be to use a static factory method to do this. Something like this instead :

public class VirtualClass {

private static List<VirtualClass> classes = new ArrayList<VirtualClass>();
private boolean isPrivate;
private String className;
private String methodName;

//A private constructor
private VirtualClass(String name, String method, boolean isP){
    this.className = name;
    this.isPrivate = isP;
    this.methodName = method;
}

private void setClassName(String className){
    this.className = className;
}

private void getClassName(){
    return className;
}

public static VirtualClass createClass(String name, String method, boolean isP){
    VirtualClass virtualClass = new VirtualClass(String name, String method, boolean isP);
    classes.add(virtualClass);
    return virtualClass;
}
}

The way your code is done now, the problems can be in a lot of places in your client classes. This structure is safer and generally used.

Also you should not type your collection with ArrayList but use the implemented interface instead, List. how to use an array list?

查看更多
登录 后发表回答