For example I can do this with a basic integer array:
int[] test1 = new int[]{2, 1, 5, 1, 3, 2};But with an Array list I have to add them one at a time like this
List<Integer> test1 = new ArrayList<Integer>(); test1.add(2); test1.add(1); test1.add(3);
?
java - Add String Array to ArrayList - Stack Overflow
Adding objects to array list - Last entry overwrites all elements
You are only creating one instance of the class Recipe. And you are adding it multiple times to the ArrayList. This will modify the object everytime.
What you need to do instead is to create a new Recipe before setting the name, and then add it to the ArrayList.
More on reddit.comWhy is the list method which returns all but the last element named init?
Just some observations:
-
head,tail,init,lastare all four-character words -
Haskell uses these same operations
-
Erlang also seems to use these terms
-
Okasaki's book/thesis also contains these, language is Standard ML
Scala borrowing from these functional languages (particularly ML and Haskell), my guess is the terminology was inspired from these.
More on reddit.comArrayList add method overwriting elements?
What symptom are you seeing which indicates the customer list is being overwritten?
The first thing that I see is inconsequential but misleading. If you do not have the new customer in the list, you print out the first customer's information to the console.
That being said, a Map<String, Double> might be a better way to do this. Or a Map<String, Customer> if you must, although until you add more fields there's not much benefit to it.
More on reddit.com