我正在尝试对数组进行排序,以便可以将数组中的元素打印到表“行”中屏幕的许多列中。我希望对数组进行如下排序:

a[0] = "Question 1"
a[1] = "Question 2"
a[3] = "Question 3"
a[4] = "Question 4"
a[5] = "Question 5"
a[6] = "Question 6"
a[7] = "Question 7"


排序为:

a[0] = "Question 1"   a[1] = "Question 5"
a[2] = "Question 2"   a[3] = "Question 6"
a[4] = "Question 3"   a[5] = "Question 7"
a[6] = "Question 4"


到目前为止,这是我的代码:

ArrayList<HolderAnswer> listAnswers = getListAnswers();
TreeMap<Integer, HolderAnswer> treeMapAnswers = new TreeMap<Integer, HolderAnswer>();
// make higher number of answers on the right
if (listAnswers.size() % NUMBER_OF_COLUMNS > 0)
  NumberInColumns++;


int count = 0;
int countOfRows = 0;

// sort by row
for (int k = 0; k < listAnswers.size(); k++) {
  for (int j = 0; j < NumberInColumns; j++) {
    if (k == 0) {
        treeMapAnswers.put((Integer) 0, listAnswers.get(k));
    } else {
        if (k % NumberInColumns > 0)
        treeMapAnswers.put((Integer) j, listAnswers.get(k));
        count++;
         }
    }

    count = count + NumberInColumns;

   }
 }


我一直在试图找出执行此操作的逻辑。请帮忙。

这是修改后的代码,仍然无法使用:

   ArrayList<HolderAnswer> listAnswers = getAnswers();
   ArrayList<ArrayList<HolderAnswer>> listAnswersSorted = new ArrayList<ArrayList<HolderAnswer>>();
   int count = 0;
   int k=0;
    for (HolderAnswer answer : listAnswers) {
        ArrayList<HolderAnswer> temp = new ArrayList<HolderAnswer>();
        temp.add(answer);
        if (k % NumberInColumns == 0 && k != 0 ) {
            listAnswersSorted.add(temp);
        }
        k++;
    }

最佳答案

除非它用于其他目的,否则您的代码会因您的操作而过于复杂。您可以直接通过listAnswers进行迭代,以使用新的排序填充另一个列表。

考虑将元素从listAnswers依次添加到新列表newList中。首先,您要添加第0个元素,然后添加第4个元素,然后添加第一个元素,然后添加第5个元素,然后添加第2个元素,然后添加第6个元素,然后添加第3个元素(后面跟第7个元素,但是在您的示例中没有第7个元素)元件)。

因此,我们已经遍历了4次,每次都将一对元素添加到新列表中(可能在最后一个循环中除外)。请注意,每对元素都偏移4,这等于listAnswers的较大一半的大小。我们可以通过(列表的大小+ 1)/ 2(如果您不确定,请尝试一些示例)来计算。

在代码中:

List<HolderAnswer> newList = new ArrayList<HolderAnswer>();

int loop = 0;
int offset = (listAnswers.size() + 1) / 2;

while (newList.size() < listAnswers.size()) {
    newList.add(listAnswers.get(loop);
    if (newList.size() < listAnswers.size()) {
        newList.add(listAnswers.get(loop + offset);
    }
    loop += 1;
}


编辑:好的,我明白你的意思了。您的代码看起来很接近,但是您需要将元素连续添加到ArrayList<HolderAnswer>中的listAnswersSorted中。可能有一种更简单的方法,但是我通过保留三个变量indexrowcol来实现,它们分别表示2D数组“ listAnswersSorted”中的listAnswers,行和列的迭代。 。

ArrayList<HolderAnswer> listAnswers = getAnswers();
ArrayList<ArrayList<HolderAnswer>> listAnswersSorted =
    new ArrayList<ArrayList<HolderAnswer>>();

// initialize the ArrayLists in listAnswersSorted
int numRows = listAnswers.size() / numColumns + 1;
for (int i = 0; i < numRows; i += 1) {
    listAnswersSorted.add(new ArrayList<HolderAnswer>());
}

// calculate column index where the "step" happens
int step = listAnswers.size() % numColumns;

// loop through and add elements to listAnswersSorted
int index = 0;
int row = 0;
int col = 0;
while (index < listAnswers.size()) {
    listAnswersSorted.get(row).add(listAnswers.get(index));

    int rows = col < step ? numRows : numRows - 1;
    row += 1;
    if (row == rows) {
    row = 0;
    col += 1;
    }
    index += 1;
}

// flatten the ArrayList<ArrayList> into a single ArrayList
ArrayList<HolderAnswer> newList = new ArrayList<HolderAnswer>();
for (ArrayList<HolderAnswer> list : listAnswersSorted) {
    newList.addAll(list);
}

10-07 15:59