更改数组中的索引位置

时间:2018-10-14 21:03:41

标签: java arrays

我对一个1d数组有疑问,而我要对此数组进行的操作是更改数组中任何值的索引值,例如

int[] num = new[2,4,6,9]

以及我要对该数组执行的操作,我希望位置0变为位置1,位置1变为0。所以该数组看起来像[4,2,6,9],该部分很容易做到,但是我很挣扎我想让数组继续沿这条路径走下去,所以[4,6,2,9]-> [4,6,9,2]令我很挣扎。到目前为止,我正在使用两个数组进行尝试,但是遇到了困难。另外,我正在尝试对所有地点(而不仅仅是第一个地点)进行此操作。

for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        temp[j] = temp2[i];
        if (j != 0) {
            temp[j - 1] = temp2[j];
        }
    }
    revert(); //I use this methods to restore any changes made so I can attempt with the next spot
} 

1 个答案:

答案 0 :(得分:0)

仅保留1个用于循环的变量和1个临时变量以交换选项卡。

private List<String> entries;

override fun onBindViewHolder(holder: TextHolder, position: Int) {
    holder.textView.text = entries[position];
}
相关问题