简体   繁体   English

如何修改一个 arraylist 而不改变另一个?

[英]How do i modify one arraylist without changing the other?

How can i modify one arraylist without changing the other.如何修改一个 arraylist 而不更改另一个。 I have two arraylists with the same type and when i make a change in the first one the change happens in the second one too.My code is something like this:我有两个具有相同类型的数组列表,当我对第一个进行更改时,第二个也会发生更改。我的代码是这样的:

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;
        }
    }
}

This is for a game and after a fight if the player wins i want to restore his health/stamina/mana back to the basic values....but when the player wins his health/stamina/mana remain the same from the last fight Here's how i tried to restore them:这是一场比赛,如果玩家获胜,我想在战斗后将他的健康/耐力/法力恢复到基本值....但是当玩家获胜时,他的健康/耐力/法力与上一场战斗保持不变这是我尝试恢复它们的方法:

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());
    }
}

Variables & objects:变量和对象:

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

And methods are pass-by-value:并且方法是按值传递的:

// 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;
}

if you use code like如果您使用类似的代码

ArrayList <Player> list1 = new ArrayList<Player>();
ArrayList <Player> list2 = list1;
list2.set(0, new Player("prmd"));

this change will change in both lists since they both pointing to same reference/address.This can also happen when you pass list as a parameter in function.此更改将在两个列表中发生更改,因为它们都指向相同的引用/地址。当您将列表作为 function 中的参数传递时,也会发生这种情况。 And the code snippet you wrote ie还有你写的代码片段,即

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

these are independent addresses so will not change each other这些是独立的地址,所以不会互相改变

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在单独的活动中从一个活动修改ArrayList - How do i modify an ArrayList from one Activity in a separate Activity 我如何创建新的ArrayList <ArrayList<Integer> &gt;在不更改旧版本的情况下从旧版本中删除? - How can I create new ArrayList<ArrayList<Integer>> from an old one without changing the old one? 我如何一个接一个地将元素添加到ArrayList中? - How do i add elements to my ArrayList one after the other? 如何依次访问arrayList中的所有元素? - How do I access all of the elements in an arrayList one after the other? Java:如何将两个变量设置为相同的值,然后更改一个而不更改另一个? - Java: How do I set two variables to the same value, then change one without changing the other? 当 ArrayList 和构造函数/getter 存储另外两个类时,如何从一个类添加到 ArrayList - How do I add to an ArrayList from one class, when the ArrayList and constructors/getters are stored 2 other classes Java-如何修改一个类中的静态ArrayList和另一个类中的ActionListener? - Java - How do I modify static ArrayList in one class with an ActionListener in another class? 如何逐个将项添加到ArrayList中而不是互相替换? - How do I add items into ArrayList one by one and not replace each other? 如何将Arraylist分配给其他2个Arraylist? - How do I distribute an Arraylist to 2 other Arraylists? 如何在不干扰其他对象的情况下操作 arrayList 中的一个对象? - How can I manipulate one object in the arrayList without messing with the other ones?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM