Think there is an array named barca[]. The array have player list. Players are determined as class. For example barca[0]=messi; messi is a player class. I want to copy barca[]. If I write barca2[]=barca[]; I copy the array but when barca2[] changes also barca[] changes. I want them independent. How can I do? ` public class Main {
public static void main(String[] args) { // TODO Auto-generated method stub Player barca[] = new Player[10]; Player barca2[] = new Player[10]; for(int i=0; i<10; i++){ barca[i] = new Player(); barca[i].id = i; } barca2 = barca; //print the arrays for(int i = 0; i<10; i++){ System.out.print(barca[i].id); } System.out.println(); for(int i = 0; i<10; i++){ System.out.print(barca2[i].id); } //change an array barca2[5].id=100; System.out.println(); //print the arrays for(int i = 0; i<10; i++){ System.out.print(barca[i].id); } System.out.println(); for(int i = 0; i<10; i++){ System.out.print(barca2[i].id); } } }
Output:
0123456789 0123456789 012341006789 012341006789 `
ArrayListbarca[]andbarca2[]is int, string or etc? can you plese provide code snippet?