1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Rep Power

    Why does my ArrayList keep overwriting its elements??

    Hi all

    I seem to have a frustrating problem - an element of my arraylist is overwritten. I have looked at many posts in many forums, but I have not managed to solve it.

    In my program (which is actually a FBDK function block algorithm implemented in Java), I want to store the data of an input variable (PART) into an element of an out variable array (PART_array). This process will happen multiple times (with the occurence of an event) and must thus store the variable in several array elements.

    The problem is that the elements of PART_array are overwritten by the last entry. For instance, for the first event occurence, PART_array[] = ["1"," "," "," "," "," "]. Then, with the second occurence, instead of PART_array[] = ["1","2"," "," "," "," "], I find PART_array[] = ["2","2"," "," "," "," "] - thus showing the overwrite. I have realised that overwrite already occurs with the storage to PART_ARRAY ArrayList. I thought that by reinitializing (p = new Part()) the problem would be solved...obviously not.

    Any help in solving this problem will be GREATLY appreciated! The code is as follows:

    package fb.rt;
    import java.util.ArrayList;
    import fb.datatype.*;
    import fb.rt.*;
    import fb.rt.events.*;
    public class STORE_TO_ARRAY extends fb.rt.FBInstance {
    /** Normal Execution Request */
    public final EventInput REQ = new EventInput(this);
    /** Execution Confirmation */
    public final EventOutput CNF = new EventOutput();
    /** EVENT NEXT */
    public final EventOutput NEXT = new EventOutput();
    /** VAR NumOfTasks:INT */
      public INT NumOfTasks = new INT();
    /** VAR current_task:INT */
      public INT current_task = new INT();
      public WSTRING PART = new WSTRING();
    /** VAR next_task:INT */
      public final INT next_task = new INT();
    /** VAR PART_array:WSTRING */
      public final ARRAY PART_array = new ARRAY(new WSTRING(), 6);
      public ArrayList<Part> PART_ARRAY = new ArrayList<Part>();
      Part p = new Part();
      //public WSTRING[] part = new WSTRING[6];
    /** The default constructor. */
    public STORE_TO_ARRAY(){
    /** The index (2) of state REQ. */
    public static final int index_REQ = 2;
    /** The actions to take upon entering state REQ. */
    private void state_REQ(){
    	eccState = index_REQ;
      public void serviceEvent(EventServer e){
    		case index_START:
    if ((e == REQ)) state_REQ();
      /** ALGORITHM REQ IN Java*/
    public void alg_REQ(){
    int ct = 0;
    ct = current_task.value;
    System.out.println("Store_to_Array triggered with current task = " + ct);
    System.out.println("NumOfTasks.value = " + NumOfTasks.value);
    if(ct <= NumOfTasks.value){
    	//write received input data to output variable array elements
    	Part p = new Part();
    	PART = new WSTRING();	
    	int nt = ct + 1;	
    	next_task.value = (short)nt;
    	if(ct == NumOfTasks.value){
    		for(int i = 0;i<= ct-1;i++){
    			PART_array.value[i] = PART_ARRAY.get(i).getPart();
    			System.out.println("PART_arraylist["+i+"] = " + PART_ARRAY.get(i).getPart().toString());
    			System.out.println("PART[] = " + PART_array.value[i].toString());
    		System.out.println("NEXT triggered");
    The class file for Part is as follows:

    package fb.rt;
    import fb.datatype.WSTRING;
    public class Part {
    WSTRING part;
    	void setPart(WSTRING part){
    		this.part = part;
    	WSTRING getPart(){
    		return part;
    Many thanks in advance!

  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Eastern Florida
    Rep Power
    I can't compile and test your code because of the third party packages it uses.

    Try debugging the code by add println statements to the code that prints out a the details of all changes that are made to the arraylist in question.

IMN logo majestic logo threadwatch logo seochat tools logo