尝试对要在表行中显示的数组进行排序 [英] Trying to sort an array to be displayed in table rows

查看:61
本文介绍了尝试对要在表行中显示的数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to sort an array so that the elements in the array can be printed out to a number of columns to the screen in table Rows. I would like the array to be sorted like this:

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"

排序为: p>

sorted to:

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"

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

Here is my code so far:

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;

   }
 }

这样做的逻辑。请帮忙。

I am stuck in trying to figure out the logic to do this. Please help.

此处已修改的代码仍然无法正常工作:

Here is modified code that still doesn't work:

   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 进行迭代,以使用新的排序填充另一个列表。

unless it's serving some other purpose, your code is overcomplicated for what you're doing. You can just iterate through listAnswers directly to populate another list with the new sort.

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

Think about adding elements from listAnswers to a new list newList in order. First, you want to add the 0th element followed by the 4th, then the 1st followed by the 5th, then the 2nd followed by the 6th, then the 3rd (which would be followed by the 7th, but in your example there is no 7th element).

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

So we've looped through 4 times, each time adding a pair of elements to the new list (except possibly on the last loop). Notice that each of the pairs of elements are offset by 4, which is equal to size of the bigger half of listAnswers. We can calculate this by taking (size of the list + 1)/2 (try a few examples if you're not convinced).

在代码中:

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

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

ok, I see what you mean. Your code looks close, but you need to add the elements consecutively to the ArrayList<HolderAnswer>s in listAnswersSorted. There may be an easier way, but I did this by keeping three variables index, row, and col, which represent the iteration through listAnswers, the row, and the column in the 2D array 'listAnswersSorted', respectively.

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

这篇关于尝试对要在表行中显示的数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆