How do I copy the contents of one ArrayList into another?
You can use such trick:
myObject = new ArrayList<Object>(myTempObject);
or use
myObject = (ArrayList<Object>)myTempObject.clone();
You can get some information about clone() method here
But you should remember, that all these ways will give you a copy of your List, not all of its elements. So if you change one of the elements in your copied List, it will also be changed in your original List.
how to copy one arrayList to another without refrencing first?
mListPreviousData = new ArrayList<>();
mListPreviousData.addAll(mAddedList);
Java ArrayList copy
Yes, assignment will just copy the value of l1
(which is a reference) to l2
. They will both refer to the same object.
Creating a shallow copy is pretty easy though:
List<Integer> newList = new ArrayList<>(oldList);
(Just as one example.)
how to deep copy one arraylist into another
As I mentioned in my comments to your question, the clone()
method returns an Object, and since the compiler can see and know only this, you must (dangerously) cast the object returned to be ArrayList for the method to work:
arrli.add((ArrayList<Integer>) arrli1.clone());
Having said this, I implore you not to use clone()
in this situation and that you read Why people are so afraid of using clone() (on collection and JDK classes)? to see why.
If this were my code, I'd make new ArrayLists objects within the for loop rather than cloning existing ones. I'm not 100% sure of what you're trying to achieve here, but perhaps something similar to:
// list to hold other lists
List<List<Integer>> listOfLists = new ArrayList<>();
// inner list to be added to above list
List<Integer> innerList = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
// if we need to create a new inner list, add old to
// list of lists and create new one
if (i != 0 && arr[i] < arr[i - 1]) {
listOfLists.add(innerList);
innerList = new ArrayList<>();
}
// always add to the current inner list
innerList.add(arr[i]);
}
// when all is done, add last inner list to list of lists
listOfLists.add(innerList);
This above code looks to be cleaner and safer. Also, if you're wondering why I'm using:
List<Integer> foo = new ArrayList<>();
rather than
ArrayList<Integer> foo = new ArrayList<>();
Please check out:
- Why do some people use the List base class to instantiate a new ArrayList? as well as
- What does it mean to “program to an interface”?
How to copy only distinct elements from one arrayList to another ArrayList
Note: This piece of code only works, because the questioner uses this if statement in his constructor:
if(n1 < n2 ){
this.num1 = n1;
this.num2 = n2;
} else {
this.num2 = n1;
this.num1 = n2;
}
I dont know if I understood your problem correctly, but try this method:
public List<TwoNumbers> doStuff(List<TwoNumbers> list) {
List<TwoNumbers> returnList = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
boolean flag = true;
for (int j = i + 1; j < list.size(); j++) {
if (list.get(i).getNum1() == list.get(j).getNum1() && list.get(i).getNum2() == list.get(j).getNum2()) {
flag = false;
}
}
if (flag) {
returnList.add(list.get(i));
}
}
return returnList;
}
This method will loop through the list and if the flag
variable is set to false we know that the object is a duplicate, so we dont have to add it to returnList
.
If you have further questions or if this didnt answer your question feel free to hit me up.
Copy one item from an ArrayList to another ArrayList Java 8
You can use get()
method to get a item at index from ArrayList
Producto product = listaProductos.get(index);
Where index is a integer
Use add()
method to add it to second list , Since the second list is of type Pedido
you can't add it unless it is of compatible type. You need to convert your Producto
object to Pedido
to do that you can create a new `Pedido' object and set all the attributes from the 'Producto' , if you have getters and setters you can do like this
Pedido pedido = new Pedido();
pedido.set...(product.get...()); //for all other fields
listaPedido.add(pedido);
If getters and setters is not written and attributes are public
you can directly assign like
pedido.attributeName = producto.attribute;
Take a look into documentation:ArrayList JavaDoc
Copy some values from one ArrayList to another kotlin
var skills: List<SkillModel> = skillsList.map { SkillModel(it.name,it.title) }
Related Topics
Installing Java on Os X 10.9 (Mavericks)
Make Hibernate Ignore Instance Variables That Are Not Mapped
Maven. Lambda Expressions Are Not Supported in -Source 1.5
How to Convert Set<String> to String[]
Compile Error: Package Javax.Servlet Does Not Exist
How to Properly Do a Background Thread When Using Spring Data and Hibernate
How to Make a Countdown Timer in Java
How to Serialize Static Data Members of a Java Class
Java Static Serialization Rules
Using Eclipse Java Compiler (Ecj) in Maven Builds
What's the Difference Between a Resource, Uri, Url, Path and File in Java
How to I Output Org.W3C.Dom.Element to String Format in Java
How to Open a Txt File and Read Numbers in Java
How to Change the Default Application Icon in Java
Why Did Servlet.Service() for Servlet Jsp Throw This Exception