user7709386
user7709386

Reputation:

adding a string to an arraylist<String>

i am having a trouble adding a string into an ArrayList. my goal is to create a list of an array using a for loop. however each time it is looping it seem always erase the previous data and then it replace it with the new data, so the ArrayList only have the value of 1 instead of 4. here is the full code

public class SerialDemo {

    private static File f;
    private static Save obj1;
    private static Save obj;

    private static int number = 4;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            //getData("newFile.txt");
            for(int i = 0; i < number;i++) {
                obj = new Save();
                obj.arrayLocationX.add(String.valueOf(10 * i));
                obj.arrayLocationY.add(String.valueOf(20 * i));
                obj.arrayTitle.add(String.valueOf(30 * i));
                obj.arrayDescription.add(String.valueOf(40 * i));
                obj.arrayNumber.add(""+i);
            }

            for(String x:obj.arrayLocationX) {
                System.out.println(obj.arrayLocationX);
            }

            //setData("newFile.txt");
            /*getData("newFile.txt");

            for(int i = 0; i < obj.arrayLocationX.size(); i++) {
                System.out.println("Value of Desc"+obj1.arrayNumber);   
                System.out.println("Value of locX"+obj1.arrayLocationX.get(i)); 
                System.out.println("Value of locY"+obj1.arrayLocationY.get(i)); 
                System.out.println("Value of Tit"+obj1.arrayTitle.get(i));  
                System.out.println("Value of Desc"+obj1.arrayDescription.get(i));   
                System.out.println("  ");
                System.out.println(obj.arrayDescription.size());
            }*/
        }catch(Exception e) {

        }

    }

    private static void setData(String fileName) {
        // set the object or data
        try {
            f = new File(fileName);
            FileOutputStream fps;
            fps = new FileOutputStream(f);
            ObjectOutputStream dos = new ObjectOutputStream(fps);
            dos.writeObject(obj);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch(IOException e) {

        }
    }
    private static void getData(String fileName) {
        // get the object or data
        try {
            f = new File(fileName);
            FileInputStream fin = new FileInputStream(f);
            ObjectInputStream Oin = new ObjectInputStream(fin);
            obj1 = (Save) Oin.readObject();
        }catch(FileNotFoundException e) {

        }catch(IOException e) {

        }catch(ClassNotFoundException e) {

        }
    }

}

class Save implements Serializable {
    ArrayList<String> arrayNumber = new ArrayList<String>();
    ArrayList<String> arrayLocationX = new ArrayList<String>();
    ArrayList<String> arrayLocationY = new ArrayList<String>();
    ArrayList<String> arrayTitle = new ArrayList<String>();
    ArrayList<String> arrayDescription = new ArrayList<String>();
}

Upvotes: 0

Views: 72

Answers (2)

Qwertycrackers
Qwertycrackers

Reputation: 666

for(int i = 0; i < number;i++) {
            obj = new Save();

obj is instantiated as a new Save every time the loop begins again. This means all your progress from the previous loop is lost and the new Save is the one that receives the inserts. You only want to create obj once, somewhere before the loop. Swapping the order of the lines should cause the program to work.

obj = new Save();
for(int i = 0; i < number;i++) {

Upvotes: 3

ThomasEdwin
ThomasEdwin

Reputation: 2145

A new obj is created in every iteration hence your added string is lost. Try moving obj = new Save(); before the loop.

Upvotes: 4

Related Questions