1

enter code hereI am new to Java, i have a class "RSTrule" with class variable "private ArrayList nucleusType". I want to set values to this arraylist in another class namely "RSTRules" i use the following code:

   public class RSTRule{
        private ArrayList<Type> nucleusType;
}

public class RSTRules extends ArrayList<RSTRule>  {



public RSTRule generateSimilarSentence()
    {
        RSTRule rstRule=new RSTRule();
        rstRule.setNucleusType(.....)
        return rstRule;

    }

}

i have two or three values for nucleus array. for example I want to add "UMM" and "UMNM" to nucleus array list in a short form. how can i do that?

3
  • 1
    I think you're mistaken use of generics. I highly doubt you need to extend ArrayList<RSTRule>. Commented Aug 14, 2013 at 12:23
  • 1
    Never extend ArrayList<E>, you should use composition over inheritance. Commented Aug 14, 2013 at 12:23
  • setNucleusType(.....) <- what are you passing here? from where do you get this? Commented Aug 14, 2013 at 12:29

7 Answers 7

1

Use Arrays.asList (no clue what UMM and UMNM are, but something like below)

rstRule.setNucleusType(Arrays.asList(UMM, UMNM));

There is a tricky part though, the ArrayList created by asList cannot be modified. If you need that, there are two options.

1: Wrap the new list in an ArrayList:

rstRule.setNucleusType(new ArrayList<Type>(Arrays.asList(UMM, UMNM)));

2: Change your setter like:

public void setNucleusType(List<Type> list) {
    nucleusType.clear();
    nucleusType.addAll(list);
}
Sign up to request clarification or add additional context in comments.

2 Comments

What about extends ArrayList<RSTRule>
@m0skit0 Does that really matter? There's quite a lot going wrong in this code, but the question seems pretty straightforward. I assumed setNucleusType is simply the setter of nucleusType in RSTRule.
0

If any existing array list with you pass it,otherwise create a new one and add elements to it and pass that.

  RSTRule rstRule=new RSTRule();
  List<Type> someList = new ArrayList<Type>();
  someList.add(UMM);   //remember the UMM is of type TYPE
  someList.add(UMNM);  //remember the UMNM is of type TYPE
  rstRule.setNucleusType(someList)
  return rstRule;

Comments

0

You can do this way

    RSTRule rstRule=new RSTRule();
    List<Type> list=new ArrayList<>();
    list.add(UMM);// add some thing in your Type
    rstRule.setNucleusType(list)
    return rstRule;

Comments

0

ArrayList is the collection class useful to store the value in the array format.

  RSTRule rstRule=new RSTRule();
  List<Type> someList = new ArrayList<Type>();
  someList.add(Value);  
  someList.add(value);  
  rstRule.setNucleusType(someList)
  return rstRule;

Comments

0
  1. Avoid having similar names RSTRule and RSTRules is not practical when you will read you program: call them RSTRule and RSTRuleList for example
  2. RSTRuleList is a good idea globally since you whant to control what is in your list depending on your business case. Therefore you can hide the list in RSTRuleList
  3. Now you use also RSTRuleList as a factory (which is not ideal: it's preferable to use a dependency injection framework, but it's ok for some needs)

    class RSTRuleList { protected ArrayList ruleList;

    public RSTRule generateSimilarSentence(... params) { ...
    return rstRule; }

    public addRule( ... params ) { ruleList.add( generateSimilarSentence(params) ); } }

Hope it helps, Best Regards, Zied

Comments

0
class RSTRuleList { 
  protected ArrayList ruleList;

  public RSTRule generateSimilarSentence(... params) { 
    ...
    return rstRule; 
  }

  public addRule( ... params ) { 
    ruleList.add( generateSimilarSentence(params) ); 
  } 
}

(The code in the above answer didn't format)

Zied

1 Comment

Zied, edit your previous answer and add this example instead of making a new answer ;)
0

Your question is not clear enough. Please see the comments and try to clarify the missing points.

You first need to add a method for RSTRule class to allow external classes to add to its internal list. Something like:

public class RSTRule {

    // You need to create the ArrayList first, 
    // and I suggest use interfaces as declaration when fit
    private final List<Type> nucleusType = new ArrayList<Type>(); 

    public void addType(final Type type) {
        this.nucleusType.add(type);
    }
}

I don't know what RSTRules is needed for...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.