ArrayList.addAll syntax
I have a serializable class that is working just fine, until I try to
read in the only serialized object (an array list) and copy it to the
original arraylist.
The class needs to be able to be used without reading in a serialized
copy, but needs the flexibility of reading one in as well.
The class is called Options and holds key/value pairs in the array
list. The following methods are available:
Options.set(String key, String value), Options.get(String key),
Options.open(String fileName), Options.save(), Options.save(String
fileName), Options.remove(String key)
Everything works fine, but when I try to call the object's .get method
after calling .open() I am thrown a NoSuchFieldException. I am certain
that there is something screwed up with the copying of the serialized
ArrayList to the existing one. I have tried looping through, copying,
..copyAll... None seem to work.
Thanks for any ideas!
Here's the code:
--------------------------------------------------------------------------
package options;
import java.io.*;
import java.util.ArrayList;
public class Options implements Serializable {
private ArrayList<String[]> preferences;
private String fileName = null;
public Options() {
preferences = new ArrayList<String[]>(10);
}
public Options(int defaultSize) {
preferences = new ArrayList(defaultSize);
}
public void open(String fileName) throws FileNotFoundException,
IOException {
FileInputStream in = new FileInputStream(fileName);
ObjectInputStream objIn = new ObjectInputStream(in);
try {
ArrayList<String[]> tempList =
(ArrayList<String[]>)objIn.readObject();
preferences.clear();
preferences.addAll(tempList);
} catch (ClassNotFoundException ex) {
throw new FileNotFoundException();
}
}
public void save(String fileName) throws IOException {
writeFile(fileName);
}
public void save() throws NoFileSpecifiedException, IOException {
if (fileName != null) {
writeFile(fileName);
} else {
throw new NoFileSpecifiedException();
}
}
public void writeFile(String fileName) throws IOException {
FileOutputStream out = new FileOutputStream(fileName);
ObjectOutputStream objOut = new ObjectOutputStream(out);
objOut.writeObject(preferences);
objOut.flush();
}
public String get(String key) throws NoSuchFieldException {
int index;
try {
index = getKeyIndex(key);
String[] keyPair = preferences.get(index);
return keyPair[1];
} catch (NoSuchFieldException ex) {
throw new NoSuchFieldException();
}
}
public void set(String key, String value) {
try {
remove(key);
} catch (NoSuchFieldException ex) {
// no need to catch, if key does not exist then
// continue and set the new key
;
}
String[] keyPair = {key, value};
preferences.add(keyPair);
}
public void remove(String key) throws NoSuchFieldException {
int keyIndex = getKeyIndex(key);
preferences.remove(keyIndex);
}
private int getKeyIndex(String key) throws NoSuchFieldException {
int index = -1;
if (!preferences.isEmpty()) {
for (int i = 0; i <= preferences.size()-1; i++) {
String[] keyPair = preferences.get(i);
if (keyPair[0]==key) { index = i; }
}
}
if (index != -1) {
return index;
} else {
throw new NoSuchFieldException();
}
}
public void showDialog(boolean modal) {
;
}
public void showDialog() {
showDialog(false);
}
}