我试图在Java中反转一个int数组。
此方法不反转数组。
for(int i = 0; i < validData.length; i++)
{
int temp = validData[i];
validData[i] = validData[validData.length - i - 1];
validData[validData.length - i - 1] = temp;
}
有什么问题吗?
我试图在Java中反转一个int数组。
此方法不反转数组。
for(int i = 0; i < validData.length; i++)
{
int temp = validData[i];
validData[i] = validData[validData.length - i - 1];
validData[validData.length - i - 1] = temp;
}
有什么问题吗?
当前回答
这里是一个简单的实现,反转数组的任何类型,加上全/部分支持。
import java.util.logging.Logger;
public final class ArrayReverser {
private static final Logger LOGGER = Logger.getLogger(ArrayReverser.class.getName());
private ArrayReverser () {
}
public static <T> void reverse(T[] seed) {
reverse(seed, 0, seed.length);
}
public static <T> void reverse(T[] seed, int startIndexInclusive, int endIndexExclusive) {
if (seed == null || seed.length == 0) {
LOGGER.warning("Nothing to rotate");
}
int start = startIndexInclusive < 0 ? 0 : startIndexInclusive;
int end = Math.min(seed.length, endIndexExclusive) - 1;
while (start < end) {
swap(seed, start, end);
start++;
end--;
}
}
private static <T> void swap(T[] seed, int start, int end) {
T temp = seed[start];
seed[start] = seed[end];
seed[end] = temp;
}
}
下面是相应的单元测试
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Before;
import org.junit.Test;
public class ArrayReverserTest {
private Integer[] seed;
@Before
public void doBeforeEachTestCase() {
this.seed = new Integer[]{1,2,3,4,5,6,7,8};
}
@Test
public void wholeArrayReverse() {
ArrayReverser.<Integer>reverse(seed);
assertThat(seed[0], is(8));
}
@Test
public void partialArrayReverse() {
ArrayReverser.<Integer>reverse(seed, 1, 5);
assertThat(seed[1], is(5));
}
}
其他回答
只是为了它。人们通常只需要反向排列的数组或列表上的“视图”,而不是在处理流和集合时完全不需要反向数组,而是原始数组/集合上的“反向”视图。,最好创建一个在列表/数组上具有反向视图的工具包。
所以创建你的Iterator实现,它接受一个数组或列表并提供输入。
/// Reverse Iterator
public class ReverseIterator<T> implements Iterator<T> {
private int index;
private final List<T> list;
public ReverseIterator(List<T> list) {
this.list = list;
this.index = list.size() - 1;
}
public boolean hasNext() {
return index >= 0 ? true : false;
}
public T next() {
if(index >= 0)
return list.get(index--);
else
throw new NoSuchElementException();
}
}
数组情况的实现非常类似。当然,迭代器也可以是流或集合的源。
因此,当你想做的只是迭代数组/列表或将其提供给流或新的集合/数组时,创建一个新数组并不总是最好的。
番石榴
使用谷歌Guava库:
Collections.reverse(Ints.asList(array));
我认为如果你声明显式变量来跟踪你在每次循环迭代中交换的下标,那么遵循算法的逻辑会更容易一些。
public static void reverse(int[] data) {
for (int left = 0, right = data.length - 1; left < right; left++, right--) {
// swap the values at the left and right indices
int temp = data[left];
data[left] = data[right];
data[right] = temp;
}
}
我还认为在while循环中执行这个操作更具可读性。
public static void reverse(int[] data) {
int left = 0;
int right = data.length - 1;
while( left < right ) {
// swap the values at the left and right indices
int temp = data[left];
data[left] = data[right];
data[right] = temp;
// move the left and right index pointers in toward the center
left++;
right--;
}
}
public void display(){
String x[]=new String [5];
for(int i = 4 ; i > = 0 ; i-- ){//runs backwards
//i is the nums running backwards therefore its printing from
//highest element to the lowest(ie the back of the array to the front) as i decrements
System.out.println(x[i]);
}
}
另一种反向数组的方法
public static int []reversing(int[] array){
int arraysize = array.length;
int[] reverse = new int [arraysize+1];
for(int i=1; i <= arraysize ; i++){
int dec= arraysize -i;
reverse[i] = array[dec];
}
return reverse;
}