我有这个多维数组:
String[][] qArray = new String[][] {
{"question", "answerOption1", "answerOption2", "answerOption3", "answerOption4"}
{"question", "answerOption1", "answerOption2", "answerOption3", "answerOption4"}
}
我将问题文本分配给TextView,将andswerOptions分配给Buttons上的文本。订单是随机选择的,因为问题不应该被问两次,我需要删除刚问的问题。我读过的我需要删除这个数组并创建一个新数组,但我不太确定这是怎么做的。我不确定for语法是如何在Java中进行的,但这是我的猜测:
假设问题是在TextView tv中进行的
String[][] newArray = new String[qArray.length-1][5] {
For rows in qArray do newArray.addRow
if (qArray[0..qArray.length][0] != tv.getText());
qArray = newArray
然后再次重做这些方法,直到没有其他问题。
答案 0 :(得分:0)
队列就是为了这个目的。
public static void main(String[] args)
{
String[][] qArray = new String[][]
{
{"question", "answerOption1", "answerOption2", "answerOption3", "answerOption4"},
{"question5", "answerOption6", "answerOption7", "answerOption8", "answerOption9"}
};
Queue<String[]> list = new LinkedList<String[]>(Arrays.asList(qArray));
for (String[] theArr = list.poll() ; theArr!=null ; theArr = list.poll())
{
for (String theStr : theArr)
{
System.out.print(theStr + " ");
}
System.out.print("\n");
}
}
答案 1 :(得分:0)
如果您只是在寻找for循环实现。为什么不使用这样的东西
String[][] newArray = new String[qArray.length][5];
for(int i=0; i<=qArray.length-1; i++){
for(int j=0; j<5; j++){
newArray[i][j] = qArray[i][j];
}
}
答案 2 :(得分:0)
public static void main(String[] args){
String[][] qArray = new String[][]{
{"question", "answerOption1", "answerOption2", "answerOption3", "answerOption4"},
{"question5", "answerOption6", "answerOption7", "answerOption8", "answerOption9"}
};
List<String[]> list = new ArrayList<String[]>(Arrays.asList(qArray));
list.remove(1);//row that needs to be deteted
String[][] qArray1 = list.toArray(new String[][]{});
for (String[] arr : qArray1) {
System.out.println(Arrays.toString(arr));
}
}