Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中从循环中的数组中删除元素_Java_Arrays - Fatal编程技术网

在Java中从循环中的数组中删除元素

在Java中从循环中的数组中删除元素,java,arrays,Java,Arrays,在从C转换到Java之后,我了解到Java包含了许多可以为您完成工作的函数,可以说,这与C中必须手动完成的操作不同 我目前正在设计一个面向对象的棋盘游戏,在这个游戏中,允许多个玩家选择一个棋子,在整个游戏中代表他们。我已经将棋子存储在一个数组中,然后让玩家的数量选择一个棋子。然而,由于明显的原因,他们不允许选择与他们之前的球员相同的比赛项目。因此,我的问题是,是否有一个函数允许我从阵列中移除一个拾取的演奏曲目,或者说我必须手动执行此操作。如果需要,我的代码如下: String[] potenti

在从C转换到Java之后,我了解到Java包含了许多可以为您完成工作的函数,可以说,这与C中必须手动完成的操作不同

我目前正在设计一个面向对象的棋盘游戏,在这个游戏中,允许多个玩家选择一个棋子,在整个游戏中代表他们。我已经将棋子存储在一个数组中,然后让玩家的数量选择一个棋子。然而,由于明显的原因,他们不允许选择与他们之前的球员相同的比赛项目。因此,我的问题是,是否有一个函数允许我从阵列中移除一个拾取的演奏曲目,或者说我必须手动执行此操作。如果需要,我的代码如下:

String[] potential_player_pieces = new String[10]; // Array storing the playing pieces available
potential_player_pieces[0]= "*";
potential_player_pieces[1]= "|";
potential_player_pieces[2]= "?";
potential_player_pieces[3]= "@";
potential_player_pieces[4]= "&";
potential_player_pieces[5]= "¬";
potential_player_pieces[6]= "!";
potential_player_pieces[7]= "%";
potential_player_pieces[8]= "<\n";

String[] player_pieces = new String[players+1]; // String to store the playing pieces that the players have chosen

for (int i=1; i<=players; i++) // Loops to the number of players and asks them what playing piece they want
{
    System.out.print("Player " + i + " pick a playing piece:"); // Asks the players the question
    for (int j=0; j<=8; j++){
        System.out.print(potential_player_pieces[j]); // Displays the possible playing pieces  
    }
    player_pieces[i] = reader.nextLine();//Saves the player chioces to the array made above
}
String[]potential_player_parties=新字符串[10];//存储可用播放曲目的阵列
潜在玩家棋子[0]=“*”;
潜在玩家棋子[1]=“|”;
潜在玩家棋子[2]=“?”;
潜在玩家棋子[3]=“@”;
潜在玩家棋子[4]=“&”;
潜在玩家棋子[5]=“;
潜在玩家棋子[6]=“!”;
潜在玩家棋子[7]=“%”;
潜在玩家棋子[8]=“我将为当前玩家介绍一个新的可用选项:

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

...

List<String> availableOptions = new ArrayList<>(
    Arrays.asList(potential_player_pieces)
);
import java.util.ArrayList;
导入java.util.array;
导入java.util.List;
...
List availableOptions=new ArrayList(
Arrays.asList(潜在玩家)
);
并在拾取完成后移除所选元素:

for (int i = 0; i < players; ++i) {
    System.out.println("Player " + i + " pick a playing piece: " + availableOptions);
    availableOptions.remove(player_pieces[i] = reader.nextLine());
}
for(int i=0;i

您还可以将数组的初始化缩短为:

String[] potentialPlayerPieces = new String[] {"*", "|", ..., "<"};

String[]potentialPlayerPieces=新字符串[]{“*”,“|”,…,”我建议使用哈希集而不是数组来存储所有棋盘碎片。一旦玩家选择了一个棋盘碎片,就可以通过调用轻松地将其从集合中移除。你也可以通过调用集合上的方法来验证选择。

我建议在你的情况下使用
哈希映射。
。我认为这种数据结构非常有效nd符合你的目的

Map<String, Integer> pieceMap = new HashMap<String, Integer>();
// HashMaps stores the value as a key value format. 
// Here the keys are the piece options 
// And the 0 is indicating that, primarily the piece is not used. 
pieceMap.put("*", 0);
pieceMap.put("|", 0);
pieceMap.put("?", 0);
pieceMap.put("@", 0);
pieceMap.put("&", 0);
pieceMap.put("¬", 0);
pieceMap.put("!", 0);
pieceMap.put("%", 0);
pieceMap.put("<\n", 0);

String[] player_pieces = new String[players + 1]; 
for (int i = 0; i < players; i++) {
    System.out.print("Player " + i + " pick a playing piece:"); // Asks the players the question

    printAvailablePieces(pieceMap);

    String piecePlayed = reader.nextLine();

    if(pieceMap.containsKey(piecePlayed) && pieceMap.get(piecePlayed).equals(0)) {
        // The piece has not used yet. 
        pieceMap.put(piecePlayed, 1);
        player_pieces[i] = piecePlayed;
    } else {
        // The piece was played before
        System.out.println("Please play a different piece");
        i--; // Take the user input again. 
    }
}

public void printAvailablePieces(HashMap<String, Integer> pieceMap) {
    for (Map.Entry<String, Integer> entry : pieceMap.entrySet()) 
        if(entry.getValue().equals(0)) System.out.print(entry.getKey() + " ");
}
Map pieceMap=newhashmap();
//HashMaps将值存储为键值格式。
//这里的关键是工件选项
//0表示基本上不使用工件。
pieceMap.put(“*”,0);
pieceMap.put(“|”,0);
pieceMap.put(“?”,0);
逐段映射。put(“@”,0);
逐段映射放置(&),0);
计件图。放置(“,”,0);
pieceMap.put(“!”,0);
pieceMap.put(“%”,0);
pieceMap.put(“使用List和HashMap

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Temp {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        List<String> potential_player_pieces = new ArrayList<String>();//Array storing the playing pieces available
             potential_player_pieces.add("*");
             potential_player_pieces.add("|");
             potential_player_pieces.add( "?");
             potential_player_pieces.add( "@");
             potential_player_pieces.add( "&");
             potential_player_pieces.add( "¬");
             potential_player_pieces.add( "!");
             potential_player_pieces.add( "%");
             potential_player_pieces.add( "<\n");
             Collections.sort(potential_player_pieces);

             System.out.println(potential_player_pieces);

             int length=potential_player_pieces.size();
             Scanner reader= new Scanner(System.in);

           Map<Integer,String> player_pieces = new HashMap<Integer,String>();//String to store the playing pieces that the players have chosen
                 for (int i=1; i<=length; i++)//Loops to the number of players and  asks them what playing piece they want
                 {
                     System.out.print("Player " + i + " pick a playing piece: ");//Asks the players the question


                     for (int j=0; j<potential_player_pieces.size(); j++){

                            if(j==0) 
                            {
                                System.out.print(potential_player_pieces.get(0)+"\n");
                                player_pieces.put(i, potential_player_pieces.get(0));
                                potential_player_pieces.remove(0);

                            }
                            if(potential_player_pieces.size()>0)
                         System.out.println(potential_player_pieces.get(j)); //Displays the possible playing pieces


                     }

                      //Saves the player chioces to the array made above

                 }

                 System.out.println(player_pieces);

    }

}
import java.util.ArrayList;
导入java.util.Collections;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入java.util.Scanner;
公共类临时工{
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
列出潜在的_player_pieces=new ArrayList();//存储可用播放片段的数组
潜在玩家棋子。添加(“*”);
潜在玩家棋子。添加(“|”);
潜在玩家棋子。添加(“?”);
潜在玩家。添加(“@”);
潜在玩家。添加(“&”);
潜在玩家棋子。添加(“,”);
潜在玩家棋子。添加(“!”;
潜在玩家棋子。添加(“%”);

潜在玩家棋子。添加(“使用集合,而不是数组。不,恐怕没有for数组,但是ArrayList或List对象有。您可以手动从数组中删除播放片段,方法是创建第二个数组,然后将第二个数组复制到原始数组中,使其看起来像已被删除一样。如果您不想使用ArrayList,则可以保留tr通过将这些棋子放入另一个数组来确认选择了哪些棋子,然后如果它在“棋子已选”数组中,就不要让它被选中。@JBNizet我不同意。OP似乎想打印每个数组元素以显示所有可能的选项。由于一组棋子是无序的,每个玩家可用棋子的顺序可能不同。如作为一个用户,我不希望出现这种行为。@Sweeper并不是所有的集合都是无序的。但是,是的,列表也可以。要点是:学习集合,不要使用数组。我可以插话吗?为什么不创建一个具有bool var“isChosenAlready”和var pieceType的playerPiece对象,它是不同类型的枚举。还有一个方法“选择画面(整数播放器)"…?IDE在第1行的列表中抛出此错误…,但其他一切都正常:不兼容的类型:无法推断ArrayList的类型参数原因:推断变量E具有不兼容的边界相等约束:字符串下界:T,ArrayList,其中E,T是类型变量:E扩展类Arr中声明的对象ayList T扩展了方法asList(T…)中声明的对象。ArrayList(Collections)的类型这里没有检查玩家是否选择了正确的棋子。你是什么意思?@BarryBBenson,应该没有任何问题,我在我的计算机上运行了它machine@ReazMurshed,OP也没有检查它,但是我们可以包装
player\u片段[i]=reader.nextLine()
使用检查ita
Map
的方法可以更好地适应,不是吗?是的,也可以。谢谢您宝贵的建议。