【问题标题】:Deleting and shrinking an ArrayList删除和缩小 ArrayList
【发布时间】:2018-03-23 18:08:12
【问题描述】:

我是 Android 新手,今天我感到非常有动力,所以我开始编写一个应用程序。

但现在我被困住了……我有一个 ArrayList,里面有字符串。使用随机数生成器,我从这个列表中得到一个字符串。 我真正想做的是避免重复字符串。我只想查看一个元素一次。

你们能帮帮我吗?

这是我的 MainActivity:

public class MainActivity extends AppCompatActivity {

private TextView welcomeText;
private TextView pokemons;
private TextView whatToDo;
private Button yesButton;
private Button noButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final String[] pokemonok = {
            "Pikachu",
            "Charizard",
            "Bulbasaur",
            "Charmander",
            "Squirtle",
            "Caterpie",
            "Metapod",
            "Weedle",
            "Rattata",
            "Onix"
    };

    final List<String> list = new ArrayList<String>(pokemonok.length);
    for (String s : pokemonok) {
        list.add(s);
    }

    welcomeText = (TextView) findViewById(R.id.welcomeText);
    pokemons = (TextView) findViewById(R.id.pokemons);
    whatToDo = (TextView) findViewById(R.id.whatToDo);
    //EditText answerText = (EditText) findViewById(R.id.answerText);
    yesButton = (Button) findViewById(R.id.yesButton);
    noButton = (Button) findViewById(R.id.noButton);

    final String[] def = {"[ " + list.get(0) + " ]"};
    pokemons.setText(def[0]);

    View.OnClickListener igen = new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Random r = new Random();
            int szam = r.nextInt(pokemonok.length);

            pokemons.setText(def[0] + "[ " + list.get(szam) + " ]");
            def[0] = def[0] + "[ " + list.get(szam) + " ]";

        }
    };

    yesButton.setOnClickListener(igen);

   }
}

【问题讨论】:

  • 所以你想每只精灵挑选一次?

标签: java android list arraylist


【解决方案1】:

如果要在访问列表后从列表中删除元素,可以调用

list.remove(i)

这将从列表中删除第 i 个元素。

【讨论】:

    【解决方案2】:

    如果您想保留该列表,您可以拥有另一个列表,并且每当您从原始列表中读取对象时,将该元素添加到临时列表中。然后,每当您想选择一个新的随机元素时,首先您必须检查该元素是否已经在临时列表中。如果是,则必须重新生成该进程。

    List<String> tempList = new ArrayList<>();
    tempList.add( // YOUR ELEMENT TO SAVE ON TEMP LIST // );
    

    然后去检查

    if (tempList.contains(element)){
          // your code to doing the process again
    {
    

    【讨论】:

      【解决方案3】:

      根据您设置随机数生成器的方式,您可以执行以下操作:

      list.remove(randomNumber);
      

      这应该从列表中删除该项目。

      【讨论】:

        【解决方案4】:

        如果您不想更改您的口袋妖怪字符串列表,那么您可以在您的类中创建一个布尔数组,用于标识已选择的口袋妖怪:

        private boolean[] pokemonPicked = new boolean[pokemonok.length];
        

        您可以将 clickListener 方法更改为:

        View.OnClickListener igen = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
        
                int r = new Random().nextInt(pokemonok.length);
                while(pokemonPicked[r]){
                    r = new Random().nextInt(pokemonok.length);
                }
                pokemonPicked[r] = true;
                int szam = r;
        
                pokemons.setText(def[0] + "[ " + list.get(szam) + " ]");
                def[0] = def[0] + "[ " + list.get(szam) + " ]";
        
            }
        };
        

        【讨论】:

        • 这似乎效率低下;在等待选择有效号码时,您可能会有任意延迟。
        • 我也不建议这样做,但实际上这工作相对较好,因为随机数是均匀分布的。在最坏的情况下,这当然会出错
        • 好吧,对于这个相对较低的数字,它可能没问题。如果你为大量的口袋妖怪(如数百万)这样做,最终会成为一个问题。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多