我如何修改一个数组列表而不更改另一个数组列表?

时间:2019-10-25 11:36:04

标签: java

如何在不更改另一个数组列表的情况下修改另一个数组列表。我有两个具有相同类型的数组列表,当我在第一个数组中进行更改时,第二个也发生了更改。我的代码是这样的:

Scanner scanner = new Scanner(System.in);
Random y = new Random();
ArrayList<Player> lista = new ArrayList<Player>();
ArrayList<Enemies> listaEnemy = new ArrayList<Enemies>();
ArrayList<Items> listaItems = new ArrayList<Items>();
ArrayList<Items> listaInventory = new ArrayList<Items>();
ArrayList<Player> basicStats = new ArrayList<Player>();

public void createCharacter(ArrayList<Player> lista,ArrayList<Player> basicStats) {
    System.out.println("Enter character name");
    String name = x.nextLine();
    showCharacterTypes();
    String type = x.nextLine();
    if(type.equalsIgnoreCase("tank")) {
        Player player = new Player(name,type,140,10,20,1,0,200,1,0);//name,type,hp,damage,defence,stamina,mana,gold
        lista.add(player);
        basicStats.add(player);
    }else if(type.equalsIgnoreCase("assassin")) {
        Player player = new Player(name,type,120,25,10,1,0,200,1,0);
        lista.add(player);
        basicStats.add(player);
    }else if(type.equalsIgnoreCase("warlock")) {
        Player player = new Player(name,type,100,30,10,0,1,200,1,0);
        lista.add(player);
        basicStats.add(player);
    }else if(type.equalsIgnoreCase("knight")) {
        Player player = new Player(name,type,120,20,20,1,0,200,1,0);
        lista.add(player);
        basicStats.add(player);
    }
}

public void enemyCombat(int enemyNO,int character,ArrayList<Player> lista) {
    boolean turn = false;
    while(!turn) {
        int action = getEnemyAction();
        if(action == 3) {
            int minValue2 = 10;
            int maxValue2 = 20;
            int enemyHp = y.nextInt(maxValue2 - minValue2)+minValue2;
            System.out.println("The enemy regenerated " + enemyHp + " health");
            listaEnemy.get(enemyNO).setEnemyHP(listaEnemy.get(enemyNO).getEnemyHP() + enemyHp);
            turn = true;
            break;
        }else if(action == 1) {
            int minValue2 = 15;
            int maxValue2 = 25;
            int damage = y.nextInt(maxValue2 - minValue2) + minValue2;
            int hit  = listaEnemy.get(enemyNO).getEnemyDamage() + damage - lista.get(character).getDefence();
            if(hit  <= 0) {
                System.out.println("");
                System.out.println("Enemy hit you for 0 damage");
            }else {
                lista.get(character).setHealth(lista.get(character).getHealth() - hit);
                System.out.println("");
                System.out.println("Enemy hit you for " + hit + " damage");
            }
            if(lista.get(character).getHealth() <= 0) {
                System.out.println("The Player has 0 health remaining");
            }else {
                System.out.println("The Player has " + lista.get(character).getHealth() + " health remaining");
            }
            System.out.println("");
            turn = true;
            break;
        }else if( action == 2) {
            int minValue2 = 20;
            int maxValue2 = 30;
            int damage = y.nextInt(maxValue2 - minValue2) + minValue2;
            int hit  = listaEnemy.get(enemyNO).getEnemyDamage() + damage - lista.get(character).getDefence();
            if(hit <= 0 ) {
                System.out.println("");
                System.out.println("Enemy hit you for 0 damage");
            }else {
                lista.get(character).setHealth(lista.get(character).getHealth() - hit);
                System.out.println("");
                System.out.println("Enemy hit you for " + hit + " damage");
            }
            if(lista.get(character).getHealth() <= 0 ) {
                System.out.println("The player has 0 health  remaining");
            }else {
                System.out.println("Player's health is equal to " +   lista.get(character).getHealth() + " health remaining");
            }
            System.out.println("");
            turn = true;
            break;
        }
    }
}

这是一个游戏,如果玩家获胜,则在打架后我想将其健康/耐力/法力恢复到基本值....但是当玩家获胜时,其健康/耐力/法力从最后一战 这是我尝试还原它们的方法:

public void restoreStats(int character,ArrayList<Player> lista,ArrayList<Player> basicStats) {
    if(lista.get(character).getType().equalsIgnoreCase("tank")) {
        lista.get(character).setHealth(basicStats.get(character).getHealth());
        lista.get(character).setStamina(basicStats.get(character).getStamina());
    }
    if(lista.get(character).getType().equalsIgnoreCase("warlock")) {
    }
    if(lista.get(character).getType().equalsIgnoreCase("assassin")) {
        lista.get(character).setHealth(basicStats.get(character).getHealth());
        lista.get(character).setStamina(basicStats.get(character).getStamina());
    }
    if(lista.get(character).getType().equalsIgnoreCase("knight")) {
        lista.get(character).setHealth(basicStats.get(character).getHealth());
        lista.get(character).setStamina(basicStats.get(character).getStamina());
    }
}

2 个答案:

答案 0 :(得分:1)

变量和对象:

List<Player> list1 = new ArrayList<>(); // list1 contains new object instance A
List<Player> list2 = new ArrayList<>(); // list2 contains new object instance B
Player player = new Player(); // player contains new object instance C
list1.add(player);
list2.add(player);
player.setPoints(13); // Changed in both lists.

List<Player> list1 = new ArrayList<>(); // list1 contains new object instance A
List<Player> list2 = list1; // list2 contains object instance A
list2.add(player);
list1.add(player);
// instance A now holds 2 Players (the same one).
assert list1.size() == 2;
assert list2.size() == 2;

List<Player> list1 = new ArrayList<>(); // list1 contains new object instance A
list1.add(player);
list1.add(player);
list1.add(player);
List<Player> list2 = list1.subList(1, 2); // list2 contains new object instance B
// But a subList is a list backed by the original list.
Player player2 = new Player();
list1.set(1, player2); // list2.get(1) == player2
Player player3 = new Player();
list2.set(1, player3); // list1.get(1) == player3

方法是按值传递的:

// list1 points to instance A
f(list1); // the value, instance A is passed
// list1 still points to instance A

void f(List<Player> x) {
    x = list2;
}

答案 1 :(得分:0)

如果您使用

之类的代码
ArrayList <Player> list1 = new ArrayList<Player>();
ArrayList <Player> list2 = list1;
list2.set(0, new Player("prmd"));

由于两个列表都指向相同的引用/地址,因此此更改将在两个列表中发生更改。当您将list作为函数中的参数传递时,也会发生这种情况。 还有您编写的代码段

ArrayList <Player> list1 = new ArrayList<Player>();
ArrayList <Player> list2 = new ArrayList<Player>();

这些是独立的地址,因此不会互相更改