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

What is the best way to read a comma delimited configuration file?
Table like java data structure
EJB-QL Query List inside Object
Sort Map<String, Object> by keys with IgnoreCase?
Java Web Project referencing another Java project
Help with Cron Jobs for Java Google App Engine
Eclipse Java project error: cannot resolve declaration in swing
Why does my SWT Shell always comes up in the background?
spring initBinder and webbindinginitializer example
Java 256-bit AES Password-Based Encryption
POI 3.2 Image Height/Width controlling
What are good InstallAnywhere replacements for installing a Java EE application?
Is Apache XML security library compatibile with IBM's JDK
Lombardi Java API
openssl des3 decrypting in java
How to fetch current year and month from MySQL in JAVA?

Categories

HOME
hive
kde
stock
syntax
react-redux
microservices
umd
ckeditor
bs4
python-unittest
windows-azure-storage
spring-tool-suite
object-detection
autoconf
java-3d
physics-engine
chromium-embedded
rundeck
p-value
sqlcipher
fish
applozic
yadcf
ioio
gtrendsr
mmenu
environment-modules
bower-install
ios5
retina-display
space-complexity
android-fingerprint-api
document.write
opshub
fedex
ruby-on-rails-3.1
filepicker
upstart
alphabet
np-complete
r-forge
pearson
zendesk-app
topbeat
nested-sets
android-fonts
vmware-tools
bonobo
mongocsharpdriver
fadeout
tform
cartesian-product
wordml
emailrelay
streambase
kineticjs
sorl-thumbnail
block-device
codeigniter-url
tt-news
google-style-guide
fpml
qcodo
device-manager
code-access-security
flask-cors
message-driven-bean
algebraic-data-types
tidy
inbox
jmapviewer
eclipse-memory-analyzer
dataadapter
ember-app-kit
referrer
google-email-migration
gdata-api
onsubmit
wse3.0
deobfuscation
appender
table-footer
appendto
android-sdk-2.1
lang
gallio
wise
ncqrs
meego
compiler-specific
adrotator
swing-app-framework
grid-system
ntvdm.exe

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App