如何从索引位置移动二维数组
<?php while ( have_posts() ) : the_post(); ?>
<h1><?php the_field('title'); ?></h1>
<p><?php the_field('description'); ?></p>
<?php if(get_field('column_description')): ?>
<div class="column_1">
<div class="column_1_image">
<?php
$col1_image =get_field('column_1_image');
?>
<img src="<?php echo $col1_image['url']; ?>"/>
</div>
<div class="column_1_description">
<p><?php the_field('column_description'); ?></p>
</div>
</div>
<?php endif; ?>
<?php if(get_field('column_2_description')): ?>
<div class="column_2">
<div class="column_2_image">
<?php
$col2_image =get_field('column_2_image');
?>
<img src="<?php echo $col2_image['url']; ?>"/>
</div>
<div class="column_2_description">
<p><?php the_field('column_2_description'); ?></p>
</div>
</div>
<?php endif; ?>
<?php endwhile;?>
任何想法?感谢
答案 0 :(得分:1)
这是使用Guava和Java 8的一种方法:
for (int[] array : x) {
Collections.rotate(Ints.asList(array), index);
}
或Java 7版本:
ID: COM-1234
Program: Swimming
Name: John Doe
Description: Joined on July 1st
------------------------------ID: COM-2345
Program: Swimming
Name: Brock sen
Description: Joined on July 1st
------------------------------ID: COM-9876
Program: Swimming
Name: johny boy
Description: Joined on July 1st
------------------------------ID: COM-9090
Program: Running
Name: justin kim
Description: Good Record
------------------------------
答案 1 :(得分:1)
这个程序使用一个逻辑,我们通过在部分中反转数组来旋转数组。首先,我们将数组反转到索引位置,然后反转剩余的数组(索引+ 1到数组的最后一个元素)。 在完成上述两个步骤之后,我们再次调用反向函数,但这次是在整个数组上,它为我们提供了所需的输出。
以下是有助于理解上述逻辑的代码。
public class ShiftTwoDArray {
public static void main(String[] args) {
int[][] x = { { 1, 2, 3, 4, 5, 6, 7 },
{ 1, 2, 3, 4, 5, 6, 7 },
{ 1, 2, 3, 4, 5, 6, 7 },
{ 1, 2, 3, 4, 5, 6, 7 }
};
int index = 3;
int i, j;
// System.out.println(x.length);
System.out.println("Before");
for (i = 0; i < x.length; i++) {
for (j = 0; j < x[i].length; j++) {
System.out.print(x[i][j] + " ");
}
System.out.println();
}
rotate(x, index);
System.out.println("\nAfter");
for (i = 0; i < x.length; i++) {
for (j = 0; j < x[i].length; j++) {
System.out.print(x[i][j] + " ");
}
System.out.println();
}
}
/**
* @param x
* @param index
* calls rotateUtil on each row
*/
private static void rotate(int[][] x, int index) {
for (int i = 0; i < x.length; i++) {
rotateUtil(x[i], index);
}
}
/**
* @param x
* @param index
* reverse array in parts and then reverse whole array
*/
private static void rotateUtil(int[] x, int index) {
reverse(x, 0, index);
reverse(x, index + 1, x.length - 1);
reverse(x, 0, x.length - 1);
}
/**
* @param x
* @param start
* @param end
* reverse an array
*/
private static void reverse(int[] x, int start, int end) {
int temp = 0;
while (start < end) {
temp = x[start];
x[start] = x[end];
x[end] = temp;
start++;
end--;
}
}
}
答案 2 :(得分:0)
这应该做你想要的:
int[][] x = { { 1, 2, 3, 4, 5, 6, 7 },
{ 1, 2, 3, 4, 5, 6, 7 },
{ 1, 2, 3, 4, 5, 6, 7 },
{ 1, 2, 3, 4, 5, 6, 7 } };
int index = 3;
// Create 2D array of matching size
int[][] y = new int[x.length][x[0].length];
// Loop through each row of x
for (int r = 0; r < x.length; r++) {
// Loop through each column of x[r][]
for (int c = 0; c < x[0].length; c++) {
// Put x's value in y, shifting to the right by index.
// See comment outside of code regarding %
y[r][(c + index) % x[0].length] = x[r][c];
}
}
// Print out y to see if it worked
for (int r = 0; r < y.length; r++) {
for (int c = 0; c < y[0].length; c++) {
System.out.print(y[r][c] + " ");
}
System.out.println();
}
此处的关键是% x[0].length
中的y[r][(c + index) % x[0].length] = x[r][c];
。 c + index
将列向右移动。但是,如果需要,% x[0].length
会将列包裹到行的开头。