0

From my created array list, the result shown once I run program is always the last item of list, repeatedly.

I created two classes, one called Array_List6 and another called Data. Array_List6 is dependent upon Data.

Please see current output: https://www.dropbox.com/s/9nb3r2hr4pm4j8r/Screen%20Shot%202016-03-15%20at%2017.57.53.png?dl=0

Below is my java code.

import java.util.ArrayList;
import javax.swing.JOptionPane;

public class Array_List6 {


  public static void main(String args[]) {

  //        Data x = new Data("Fred",41);
  //        x.Print();


    // Using an Array List. 
    // Create an ArrayList of objects.

            ArrayList<Data> array_list = new ArrayList<Data>(); 

            array_list.add(new Data("Fred", 21));
            array_list.add(new Data("Joe", 43));                
            array_list.add(new Data("Zoe", 37));
            array_list.add(2, new Data("Harry", 78)); 

//     Display our objects.

            PrintDataArray(array_list);

        //conclusion: duplicating last result in array. i.e last person Harry for example is displayed 4 times instead of Fred, Jo, Zoe then Harry.

System.out.println("==================5.5=====================");   


    //5.5 - ArrayList Implementation Considerations

            //Array A
    /*-*/       ArrayList<Data> ArrayA = new ArrayList<Data>(); 

                ArrayA.add(new Data("Fred", 21));
                ArrayA.add(new Data("Joe", 43));            
                ArrayA.add(new Data("Zoe", 37));

            //ArrayB
    /*-*/       ArrayList<Data> ArrayB = new ArrayList<Data>();

                // Display our objects.

                PrintDataArray(ArrayA);
                System.out.println();   
                ArrayB = ArrayA;
                PrintDataArray(ArrayB);
                System.out.println();   
                ArrayA.remove(1);
                PrintDataArray(ArrayB);

                System.out.println("-------------------------------------");    

    /*-*/       ArrayList<Data> ArrayC = new ArrayList<Data>(); 

                ArrayC.add(new Data("Fred", 21));
                ArrayC.add(new Data("Joe", 43));            
                ArrayC.add(new Data("Zoe", 37));

                //cloning an array
                System.out.println("Cloning an array.");
                System.out.println();

    /*-*/       ArrayList<Data> ArrayD = new ArrayList<Data>();

                PrintDataArray(ArrayC);
                System.out.println();

                ArrayD = (ArrayList<Data>)ArrayC.clone();
                ArrayC.remove(1);
                PrintDataArray(ArrayC);
                System.out.println();   

                PrintDataArray(ArrayD);
                System.out.println();                                   

    //Deleting elements from ArrayA and ArrayC and displaying the elements from ArrayB and 
    //ArrayD respectively. 


            } //main



private static void PrintDataArray(ArrayList<Data> array) 
            {
                for(int i=0;i<array.size();++i)
                {
                    array.get(i).Print();
                }
            }

private static void PrintArray(int[][] array) 
            {
                for(int i=0;i<array.length;++i)
                {
                    for(int j=0;j<array[i].length;++j)
                    {
                        System.out.print(array[i][j] + " ");
                    }
                    System.out.println();
                }
            }


    }


} //class

This is the Data Class.

public class Data {

private static String name;
private static int age;
Data(String n,int a)

{
    name = n;
    age = a;
}


public static String GetName()
{
    return(name);
}


public static void SetName(String n)

{
    name = n;
}


public static int GetAge()

{
    return(age);
}

public static void SetAge(int a)

{
    age = a;
}

public static void Print()

{   
    System.out.println(("("+GetName() + "," + GetAge() +")" ));
}

}
Fox
  • 33
  • 8

3 Answers3

3

Well your Data class just has static members. Thats the problem about it. You need instance members. A static variable is shared through all instances (it exists once per program execution). You might want to take a look at Oracles "Understanding Class Members" page. As another illustration this diagram about program memory and when it's allocated might help understanding.

Change to:

public class Data{

private String name;
private int age;

public Data(String name, int age){
this.name = name;
this.age = age;
}

public String getName(){
return this.name;
}

[... Other getters and setters...]
}
Andreas Brunnet
  • 687
  • 6
  • 18
1

I think you've made a critical error in your Data class. Everything in it, namely name and age, is static. This means it is shared across all instances of Data objects. In fact, apart from the constructor, you have no non-static methods at all, which means that every single Data is identical, and each time you create a new one, you're actually modifying name and age for every single instance of Data.

To learn more about what this means, see I want to know the difference between static method and non-static method .

Community
  • 1
  • 1
childofsoong
  • 1,809
  • 13
  • 22
0

Make your members non static. When they are static they are the same for all instances of your Data class.

https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

Samvel Petrosov
  • 6,842
  • 2
  • 16
  • 44