【问题标题】:Can Android Studio remove multiple elements from an array? How?Android Studio 可以从数组中删除多个元素吗?如何?
【发布时间】:2019-03-20 07:33:25
【问题描述】:

我正在练习开发应用,但遇到了问题。我想从数组中删除两个元素,但android studio只能删除一个,如果我再添加一个,应用程序将崩溃,下面是我的代码,现在我只输入一个代码来删除第一个元素,否则我需要删除最后一个元素答案的选择将混合数组。

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

String quizData[][] = {
        {"quadrilateral","quadrilateral","triangle","heptagon","pentagon","hexagon","decagon","Which one is the green polygon just now ?"},
        {"triangle","triangle","heptagon","quadrilateral","pentagon","octagon","decagon","Which one is the red polygon just now ?"},
        {"circle","circle","triangle","quadrilateral","pentagon","nonagon","decagon","Which one is the black polygon just now ?"},
        {"star","star","circle","octagon","pentagon","hexagon","decagon","Which one is the brown polygon just now ?"},
        {"decagon","decagon","triangle","circle","pentagon","hexagon","star","Which one is the pink polygon just now ?"},
        {"heptagon","heptagon","decagon","quadrilateral","pentagon","hexagon","star","Which one is the yellow polygon just now ?"},
        {"hexagon","hexagon","decagon","heptagon","pentagon","octagon","star","Which one is the sky blue polygon just now ?"},
        {"nonagon","nonagon","circle","quadrilateral","triangle","hexagon","star","Which one is the orange polygon just now ?"},
        {"pentagon","pentagon","nonagon","octagon","heptagon","hexagon","circle","Which one is the purple polygon just now ?"},
        {"octagon","octagon","triangle","quadrilateral","pentagon","hexagon","star","Which one is the dark blue polygon just now ?"}
};

public void Show_Next_Quiz(){

        Random random = new Random();
        int Random_Num = random.nextInt(quizArray.size());

        ArrayList<String> quiz = quizArray.get(Random_Num);

        Question1.setText(quiz.get(7));

    iV1.setImageResource(
            getResources().getIdentifier(quiz.get(0), "drawable", getPackageName())
    );
    iV2.setImageResource(
            getResources().getIdentifier(quiz.get(0), "drawable", getPackageName())
    );
    iV3.setImageResource(
            getResources().getIdentifier(quiz.get(0), "drawable", getPackageName())
    );
    iV4.setImageResource(
            getResources().getIdentifier(quiz.get(0), "drawable", getPackageName())
    );
    iV5.setImageResource(
            getResources().getIdentifier(quiz.get(0), "drawable", getPackageName())
    );
    iV6.setImageResource(
            getResources().getIdentifier(quiz.get(0), "drawable", getPackageName())
    );

        Right_Answer = quiz.get(1);
        quiz.remove(0);
        //quiz.remove(7);

        Collections.shuffle(quiz);

        ans1.setText(quiz.get(1));
        ans2.setText(quiz.get(2));
        ans3.setText(quiz.get(3));
        ans4.setText(quiz.get(4));
        ans5.setText(quiz.get(5));
        ans6.setText(quiz.get(6));

        quizArray.remove(Random_Num);

}

【问题讨论】:

标签: java android arrays arraylist


【解决方案1】:

您的程序崩溃了,因为您尝试使用不正确的项目位置从列表中删除项目。

例如,假设您有一个大小为 7 的 ArrayList:

ArrayList<String> quizzes = new ArrayList<>(7);

测验中的测验项目将在索引位置的0 - 6 范围内。当您删除第一个项目时:

quizzes.remove(0);

现在的测验大小是 6。现在您只能从0 to 5 索引位置访问项目。当您尝试删除项目位置超出此范围的项目时,它会出错。因此,以下代码将不起作用并给您带来崩溃:

quizzes.remove(7);

因为您正在尝试访问不存在的项目位置。

因此,您不能依赖项目位置来删除您的案例中的项目。您可以改用public boolean remove(Object o)。

quizzes.remove("triangle");

您可能需要另一个 ArrayList 用于 ansX 视图以匹配问题的大小。像这样的:

int sizeOfTheQuiz = 6;
ArrayList<String> quizzes = new ArrayList<>(sizeOfTheQuiz);

ArrayList<TextView> tvAnswers = new ArrayList<>();
tvAnswers.add(ans1);
tvAnswers.add(ans2);
tvAnswers.add(ans3);
tvAnswers.add(ans4);
tvAnswers.add(ans5);
tvAnswers.add(ans6);

// size of the quizzes must not larger than tvAnswers
for(int i = 0; i < quizzes.size(); i++) {
  tvAnswers.get(i).setText(quizzes.get(i));
}

【讨论】:

  • 噢谢谢!!你帮了我很多!
  • 太棒了!请查看当前答案以获取更多详细信息。
猜你喜欢
  • 2022-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-17
  • 1970-01-01
  • 1970-01-01
  • 2021-04-09
  • 2012-04-27
相关资源
最近更新 更多