Android Studio可以从数组中删除多个元素吗?怎么样?

时间:2019-03-20 07:33:25

标签: java android arrays arraylist

我正在练习开发应用程序,但是遇到了问题。我想从数组中删除两个元素,但是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);

}

1 个答案:

答案 0 :(得分:0)

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

例如,假设您有一个大小为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");

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

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