Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unable to copy List<gnrc> to another List<gnrc>???

Status
Not open for further replies.

sapatos

Programmer
Jan 20, 2006
57
AU
I have a utility class "Utility" that contains two lists:

private static final List<SpreadSheetRecord> DISPLAY = new ArrayList<SpreadSheetRecord>();
private static final List<SpreadSheetRecord> PREVLIST = new ArrayList<SpreadSheetRecord>();

These have getter methods:

public static List<SpreadSheetRecord> getDisplayList() {

return DISPLAY;
}

public static List<SpreadSheetRecord> getPreviousInstanceSSList() {

return PREVLIST;
}

PREVLIST is populated with a list of beans (SpreadSheetRecord) in a static{} block of the Utility class, whilst DISPLAY is populated in another class.

This other class (jframe) gets these references thus:

List<SpreadSheetRecord> displayList = Utility.getDisplayList();
List<SpreadSheetRecord> previousList = Utility.getPreviousInstanceSSList();

At this point previousList could have any number of elements and displayList has none. During execution I need to copy the elements from previousList into displayList based on user request, however I get errors saying that displayList is not big enough. ok so I change it from Collections.copy to a for loop:

for (SpreadSheetRecord s: previousList) {
displayList.add(s);
}

However I get further errors, it won't let me. The only work around I could find was in the Utility class in the static{} block to load the DISPLAY List with say 2000 new objects prior importing to the other class. Then prior to loading in the for loop i call display.List.clear();

It then works fine??? Can someone explain whats going on here as this work around is pants and from some testing I've found that I have to initialise the DISPLAY List to more than the anticipated number I'm going to copy in from previousList.

It works but its not pretty.

Any help would be great.

 
That was the first thing I tried, however the dest collection has to be at least as big as the source for it to work. I.e

Collections.copy(Collection a{15 elements}, Collection b{14});

won't work as b doesn't have the capacity to hold a. I found the solution somewhere on the web which was to loop through:

Colleciton c;
for (Collection a: b) {

c.add(a);
}

for example. So I tried this and it failed. The only way I could get it to work was to load up collection b with say 2000 objects in the Utility class. Get a reference to it in the main class and:

c.clear();
for (Collection a: b) {

c.add(a);
}

This is weird and I've not been able to find out an explanation for it? Loading up 2000 objects seems extreme but I found that loading it with anything less than the amount that was to be copied would cause a failure even though it was cleared down first before loading in the for loop. As I don't know how many objects might be loaded I had to add a lot more than might appear necessary.
 
Which exception do you get by adding the elements? Collection is an interface so, which is the actual supporting class?

Cheers,
Dian
 
My apologies that was only pseudocode:

private static final List<SpreadSheetRecord> DELTA = new ArrayList<SpreadSheetRecord>();

Its a List = ArrayList<> as above. Without loading the DELTA List with empty objects as in below:

for (int i = 0; i < 2000; i++) {
DISPLAY.add(new SpreadSheetRecord());
}

I get the following error:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:546)
at java.util.ArrayList.get(ArrayList.java:321)
at org.jdesktop.swingbinding.impl.ListBindingManager$ColumnDescriptionManager.validateBinding(ListBindingManager.java:191)
at org.jdesktop.swingbinding.impl.ListBindingManager.valueAt(ListBindingManager.java:99)
at org.jdesktop.swingbinding.JTableBinding$BindingTableModel.getValueAt(JTableBinding.java:713)
at javax.swing.JTable.getValueAt(JTable.java:1903)
 
But that means the error comes getting data form the original list, not adding them to the target.

You have to iterate to the list size.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top