java


Java memory space overlap?


I have a problem of adding elements into an ArrayList. Each time I do the 'add', the entire array content is replaced with the current element value. I end up with eg. 10 repeated element duplicates.
The classes are set up as follows:
public class BradfordReport {
EmployeeRow _empRow = new EmployeeRow();
ArrayList<EmployeeRow> _bradfordData = new ArrayList<EmployeeRow>();
public void Run() {
// processing to setup Employee row variables
for (int x=0; x<10; x++) {
// This next line in debug IS ADJUSTING THE ARRAYLIST DATA!!
_empRow.EmpNum = x; // etc for other variable in EmployeeRow
_bradfordData.add(er);
}
}
// THE RESULT IN _bradfordData is 10 elements, all with EmpNum = 10!
}
public class EmployeeRow {
int EmpNum;
string EmpNm; // etc.
}
Am I getting Java memory allocation confused here? It appears that EmployeeRow variable and the ArrayList are sharing the same memory space - very peculiar. Thanks guys
You are adding the same instance of the EmployeeRow class to the arraylist. Try something like:
public class BradfordReport {
EmployeeRow _empRow = new EmployeeRow();
ArrayList<EmployeeRow> _bradfordData = new ArrayList<EmployeeRow>();
public void Run() {
// processing to setup Employee row variables
for (int x=0; x<10; x++) {
// create a NEW INSTANCE of an EmployeeRow
_empRow = new EmployeeRow();
_empRow.EmpNum = x; // etc for other variable in EmployeeRow
_bradfordData.add(_empRow);
}
}
// THE RESULT IN _bradfordData is 10 elements, all with EmpNum = 10!
}
public class EmployeeRow {
int EmpNum;
string EmpNm; // etc.
}
Only one EmployeeRow object is every created.
Then it is modified. "It" being "the same object". If a new object is desired, then create a new object :)
Happy coding.
Yes when you do
_empRow.EmpNum = x;
you are changing the objects internal variable. You need to construct a new object each time. Inside the loop do something like this:
EmployeeRow _empRow = new EmployeeRow();
_empRow.EmpNum = x;
_bradfordData.add(_empRow);
You aren't creating new rows so every element is the same and since the loop ends at ten the last object has a value of ten.
public class BradfordReport {
EmployeeRow _empRow = new EmployeeRow();
ArrayList<EmployeeRow> _bradfordData = new ArrayList<EmployeeRow>();
public void Run() {
// processing to setup Employee row variables
for (int x=0; x<10; x++) {
// This next line in debug IS ADJUSTING THE ARRAYLIST DATA!!
_empRow = new EmployeeRow();
_empRow.EmpNum = x; // etc for other variable in EmployeeRow
_bradfordData.add(er);
}
}
// THE RESULT IN _bradfordData is 10 elements, all with EmpNum = 10!
}

Related Links

Outbound channel adapters in the same transaction
can we add multiple rows to existing table in word template using java
How to launch a <provider/> tag (Launching Shazam search activity)
“NoNodeAvailableExceptionException” thrown by class TransportClient
Transform AmazonS3 send to RX android observable
Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference [duplicate]
For what is the symbol $ after a variable in Java?
index_jsp.java uses or overrides a deprecated API. Recompile with -Xlint:deprecation for details
Batch error sending push: Unable to send your test message: This push token was registered with a Sender ID that doesn't match this authorization key
Firebase populating recyclerview error: com.firebase.client.FirebaseException: Failed to bounce to type
Align lines with opening and closing brackets
OSGI DS: declarative services work without xml file
App crashes when nothing is inputted when I click save button. Error should pop up
The type java.io.ObjectInputStream cannot be resolved. It is indirectly referenced from required .class files
Update spinner values from database
RoundingMode HALF_UP is not working properly

Categories

HOME
pandas
pypi
push-notification
nullpointerexception
homebrew
lodash
sd-card
mean-stack
razor
rdf
pheatmap
reveal.js
kentor-authservices
izpack
size
hex-editors
visual-studio-cordova
lombok
dosgi
flux
normalizr
interop
beyondcompare
scriptcs
abi
tokenize
orleans
ehcache
dcevm
mustache.php
realex-payments-api
bootstrap-tour
smb
social-media
elasticsearch-ruby
xacml
windows-server-2000
nameservers
yadcf
cloud-code
mpmediaquery
git-merge
retina-display
espeak
withings
vapor
dism
jvm-languages
modelmapper
angular2-meteor
filepicker
sage-one
domain-model
statsd
mplayer
carrot
chord-diagram
angular-strap
jquery-filter
firmata
google-feed-api
heidisql
gridpane
dstu2-fhir
google-web-starter-kit
merge-conflict-resolution
player
multiple-regression
fputcsv
lttng
superstack
associative-array
pyke
cartesian-product
mutation-observers
simple-framework
libressl
iis-arr
website-monitoring
tween
c3
system.net.webexception
mysql-error-1062
.aspxauth
soundtouch
jplaton
dataadapter
mylyn
hungarian-algorithm
mqx
interface-orientation
limejs
dice
runas
hosts-file
regsvr32
postgresql-performance
external-accessory
invite
coda-slider
web-application-design
user-friendly
privilege
private-members
.net-1.0

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App