2016-06-13 53 views
1

[编辑]相当新的网站,以便试图澄清问题。删除方法中的ArraList元素

我正在一个项目中,我有一个方法,其中一个参数是一个数组。

实施例:

public class Match 

public void playMatch(int teamA, int teamB, ArrayList<String> groups) 
Random scoreA = new Random(); 
int score1 = scoreA.nextInt(5) + 0; 
int score2 = scoreA.nextInt(5) + 0; 
System.out.println("Qtr 1: " + score1 + " " + score2); 

if (score1 > score2){ 
    System.out.println(groups.get(teamA) + " win " + score1 + " to + 
    score2 + " " + teams.get(teamB) + " eliminated."); 
      teams.remove(teamB); 
} 
else if (score2 > score1){ 
    System.out.println(groups.get(teamB) + " win " + score2 + " to " + 
    score11 + " " + teams.get(teamA) + " eliminated."); 
      teams.remove(teamA);} 
} 
public static void main(String[] args) { 
Match game1 = new Match(); 
ArrayList<String> groups = new ArrayList<String>(
      Arrays.asList("team1", "team2", "team3")); 
System.out.println("Round 1"); 
game1.playMatch(0, 1, groups); 

的问题是,当我删除元件在我匹配方法不从在我的主要方法ArrayList中移除。这是一个问题,因为我希望能够然后做:

game2.playMatch(0, 1, groups)

其中1 = team3而不是团队2.

我怎样才能让这个当我删除从一个元素数组在我的匹配方法,它实际上会从我的主要方法中的数组中删除该元素?这甚至有可能吗?如果可能的话,我宁愿有适合我的代码的东西,而且我不想实现另一种方法,如果我不需要,因为实际上比这更多,但是这给了什么我想要发生。

+2

,你能不能给我们一个[MCVE] – Gendarme

+0

请注意,数组和ArrayList是两个不同的东西。 – Gendarme

+0

'exampleArray.remove(1);'应该可能是'exampleArray.remove(elem1);' –

回答

0

我重构了您给出的内容,下面的内容适用于我的目的,并且似乎是按照您的要求做的。修复了一堆语法错误和格式化的东西。看起来你没有考虑关系score1 == score2。请尝试下面的代码,以我添加到您的主法修改前后的名单打印出来的笔记,看看它是否有助于

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 
import java.util.Random; 

public class MyMatch { 

    public void playMatch(int teamA, int teamB, List<String> groups) { 
     Random rand = new Random(); 
     int score1 = rand.nextInt(5) + 0; 
     int score2 = rand.nextInt(5) + 0; 
     System.out.println("Qtr 1: " + score1 + " " + score2); 

     if (score1 > score2){ 
      System.out.println(groups.get(teamA) + " win " + score1 + " to " + 
      score2 + " " + groups.get(teamB) + " eliminated."); 
      groups.remove(teamB); 
     } else if (score2 > score1){ 
      System.out.println(groups.get(teamB) + " win " + score2 + " to " + 
      score1 + " " + groups.get(teamA) + " eliminated."); 
      groups.remove(teamA); 
     } else if (score1 == score2) { 
      System.out.println(groups.get(teamA) + " tie " + score1 + " to " + 
      groups.get(teamB) + " " + score2 + " no team was eliminated."); 
     } 
    } 

    public static void main(String[] args) { 
     MyMatch game1 = new MyMatch(); 
     List<String> groups = new ArrayList<String>(Arrays.asList("team1","team2", "team3")); 
     System.out.println("Round 1"); 
     System.out.println("BEFORE: " + groups); 
     game1.playMatch(0, 1, groups); 
     System.out.println("AFTER: " + groups); 
    } 
} 
+0

这工作,为什么这只是从ArrayList语法切换到列表虽然?我的意思是,这实际上是我通过这个答案所做的,并且它在ArrayList没有的地方起作用。 – Smackelbap

+0

这不是现在它工作的原因。这是另一篇关于多态性的长篇讨论,对于这篇文章来说太长了。这是一个很好的链接。 http://www.tutorialspoint.com/java/java_polymorphism.htm。无论如何,你的代码中有很多语法错误(比如缺少花括号,错误地命名像score11和团队的变量,缺少第一个打印标记中的引号等等),更不用说格式化了,所以很难阅读。你是否在使用类似eclipse的IDE?如果是这样,它会捕获代码中的编译错误。除此之外,你的代码很好! –

+0

ArrayList是一个列表。 Java(JVM)计算出它在运行时List的哪个实现(ArrayList,LinkedList等)。如果在方法中使用List 作为参数类型而不是ArrayList ,这是一个最佳实践,因为它使您的代码能够灵活地接受List的不同实现,从而使您的代码更具可伸缩性,并且可以在以后重用。你也可以用List来实例化你的List,而不是ArrayList作为你声明的类型(对象赋值的左边)。合理? –

0

在下面的例子中,您将传递removeByIndex方法的三个参数:a(给定索引),b(另一个给定索引)和arrayList(元素的容器)。然后,它将创建容器的副本(克隆),在a和b中找到最低索引,从arrayList中删除该索引处的元素,并返回修改后的副本。

public static ArrayList<String> removeByIndex(int a, int b, ArrayList<String> arrayList) { 

    ArrayList<String> clone = (ArrayList<String>) arrayList.clone(); 

    int minIndex = Math.min(a, b); 

    clone.remove(minIndex); 

    return clone; 
} 

public static void main(String[] args) { 

    ArrayList<String> example = new ArrayList<String>(); 

    example.add("One"); 
    example.add("Two"); 
    example.add("Three"); 
    example.add("Four"); 
    example.add("Five"); 

    System.out.println(example); 

    example = removeByIndex(1, 3, example); 

    System.out.println(example); 
} 

请注意,您必须将该方法的返回值指定给容器,否则将不会被修改。

+0

我对这个可怜的问题格式表示抱歉,我对这个网站相当陌生。我希望这更清楚。 – Smackelbap

+0

@Marcelo Vinicius:嗯....你能帮我理解为什么你要克隆arrayList吗? –

+0

将克隆技术转化为一种方法不是一回事吗?我正在这样做,以便我可以调用该方法。如果我有50个团队,我可以在初始化ArrayList并用团队填充之后调用该方法50次,而不是写入数百行代码。这是你问的吗? – Smackelbap