Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 22nd, 2012, 10:31 PM
KarelKruger KarelKruger is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 KarelKruger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 55 m 4 sec
Reputation Power: 0
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:

Code:
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();

/** VAR PART:WSTRING */
  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(){
    super();
  }

/** 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;
	alg_REQ();
	serviceEvent(null);}

  public void serviceEvent(EventServer e){
	switch(eccState){
		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
	p.setPart(PART);
	PART_ARRAY.add(ct-1,p);
	
	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());
		}
		CNF.serviceEvent(this);
	}
	else{
		NEXT.serviceEvent(this);
		System.out.println("NEXT triggered");
	}
}
}
}


The class file for Part is as follows:

Code:
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!

Regards
Karel

Reply With Quote
  #2  
Old November 23rd, 2012, 07:30 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Why does my ArrayList keep overwriting its elements??

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap