java 怎么交换同一数组里面的两部分元素的位置 像 {1,2,3,4,5,6} 最后结果是{4 50
java怎么交换同一数组里面的两部分元素的位置像{1,2,3,4,5,6}最后结果是{4,5,6,1,2,3}把前半部分和后半部分交换...
java 怎么交换同一数组里面的两部分元素的位置
像 {1,2,3,4,5,6}
最后结果是{4,5,6,1,2,3}把前半部分和后半部分交换 展开
像 {1,2,3,4,5,6}
最后结果是{4,5,6,1,2,3}把前半部分和后半部分交换 展开
4个回答
展开全部
public class TDemo {
public static void main(String[] args) {
int []arr= {1,2,3,4,5,6};
exchange(arr,3);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
/**
* @param arr 需要交换的数组
* @param index 交换的位置
*/
private static void exchange(int[] arr,int index) {
int []temp=new int[arr.length];
int t=0;
for (int i = index; i < arr.length; i++) {
temp[t++]=arr[i];
}
for (int i = 0; i < arr.length-index; i++) {
temp[t++]=arr[i];
}
for (int i = 0; i < temp.length; i++) {
arr[i]=temp[i];
}
}
}
展开全部
假设你的数组元素个数是偶数。。
i指向最开始的1,j指向中间元素(比如4),然后交换a[i]和a[j],然后i++和j++,再继续交换a[i]和a[j],一直重复到 j 达到最后。
void swap(int[] a) {
int i = 0, j = a.length / 2;
for(; j<a.length; i++, j++) {
int tmp = a[i];
a[j] = tmp;
a[i] = a[j];
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2015-11-25
展开全部
import java.util.Arrays;
public class BaiduZhidao {
/**
* @param args the command line arguments
*/
public int[] randomCommon(int min, int max, int n) {
if (n > (max - min + 1) || max < min) {
return null;
}
int[] result = new int[n];
int count = 0;
while (count < n) {
int num = (int) (<a href="https://www.baidu.com/s?wd=Math.random&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1dBP1N-njDsnAnsuHDvm1Ts0AP8IA3qPjfsn1bkrjKxmLKz0ZNzUjdCIZwsrBtEXh9GuA7EQhF9pywdQhPEUiqkIyN1IA-EUBtkrj61nHT4PWnLnHfkPHb3nH03" target="_blank" class="baidu-highlight">Math.random</a>() * (max - min)) + min;
boolean flag = true;
for (int j = 0; j < n; j++) {
if (num == result[j]) {
flag = false;
break;
}
}
if (flag) {
result[count] = num;
count++;
}
}
return result;
}
public static void main(String[] args) {
int a[] = {1, 2, 3, 4, 5, 6};
int index[] = new int[3];
index = new BaiduZhidao().randomCommon(0, 7, 3);
int r1[] = new int[6];
int r2[] = new int[6];
int r3[] = new int[6];
int r4[] = new int[6];
for (int i = 0; i < index.length; i++) {
index[i] = index[i] - 1;
}
<a href="https://www.baidu.com/s?wd=Arrays.sort&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1dBP1N-njDsnAnsuHDvm1Ts0AP8IA3qPjfsn1bkrjKxmLKz0ZNzUjdCIZwsrBtEXh9GuA7EQhF9pywdQhPEUiqkIyN1IA-EUBtkrj61nHT4PWnLnHfkPHb3nH03"target="_blank" class="baidu-highlight">Arrays.sort</a>(index);
r1 = Arrays.copyOf(a, a.length);
r2 = Arrays.copyOf(a, a.length);
r3 = Arrays.copyOf(a, a.length);
r4 = Arrays.copyOf(a, a.length);
r1[index[0]] = a[index[1]];
r1[index[1]] = a[index[0]];
r1[index[2]] = a[index[2]];
r2[index[0]] = a[index[1]];
r2[index[1]] = a[index[2]];
r2[index[2]] = a[index[0]];
r3[index[0]] = a[index[2]];
r3[index[1]] = a[index[0]];
r3[index[2]] = a[index[1]];
r4[index[0]] = a[index[2]];
r4[index[1]] = a[index[1]];
r4[index[2]] = a[index[0]];
for (int i = 0; i < r1.length; i++) {
System.out.print(r1[i] + " ");
}
System.out.println();
for (int i = 0; i < r1.length; i++) {
System.out.print(r2[i] + " ");
}
System.out.println();
for (int i = 0; i < r1.length; i++) {
System.out.print(r3[i] + " ");
}
System.out.println();
for (int i = 0; i < r1.length; i++) {
System.out.print(r4[i] + " ");
}
}
}
public class BaiduZhidao {
/**
* @param args the command line arguments
*/
public int[] randomCommon(int min, int max, int n) {
if (n > (max - min + 1) || max < min) {
return null;
}
int[] result = new int[n];
int count = 0;
while (count < n) {
int num = (int) (<a href="https://www.baidu.com/s?wd=Math.random&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1dBP1N-njDsnAnsuHDvm1Ts0AP8IA3qPjfsn1bkrjKxmLKz0ZNzUjdCIZwsrBtEXh9GuA7EQhF9pywdQhPEUiqkIyN1IA-EUBtkrj61nHT4PWnLnHfkPHb3nH03" target="_blank" class="baidu-highlight">Math.random</a>() * (max - min)) + min;
boolean flag = true;
for (int j = 0; j < n; j++) {
if (num == result[j]) {
flag = false;
break;
}
}
if (flag) {
result[count] = num;
count++;
}
}
return result;
}
public static void main(String[] args) {
int a[] = {1, 2, 3, 4, 5, 6};
int index[] = new int[3];
index = new BaiduZhidao().randomCommon(0, 7, 3);
int r1[] = new int[6];
int r2[] = new int[6];
int r3[] = new int[6];
int r4[] = new int[6];
for (int i = 0; i < index.length; i++) {
index[i] = index[i] - 1;
}
<a href="https://www.baidu.com/s?wd=Arrays.sort&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1dBP1N-njDsnAnsuHDvm1Ts0AP8IA3qPjfsn1bkrjKxmLKz0ZNzUjdCIZwsrBtEXh9GuA7EQhF9pywdQhPEUiqkIyN1IA-EUBtkrj61nHT4PWnLnHfkPHb3nH03"target="_blank" class="baidu-highlight">Arrays.sort</a>(index);
r1 = Arrays.copyOf(a, a.length);
r2 = Arrays.copyOf(a, a.length);
r3 = Arrays.copyOf(a, a.length);
r4 = Arrays.copyOf(a, a.length);
r1[index[0]] = a[index[1]];
r1[index[1]] = a[index[0]];
r1[index[2]] = a[index[2]];
r2[index[0]] = a[index[1]];
r2[index[1]] = a[index[2]];
r2[index[2]] = a[index[0]];
r3[index[0]] = a[index[2]];
r3[index[1]] = a[index[0]];
r3[index[2]] = a[index[1]];
r4[index[0]] = a[index[2]];
r4[index[1]] = a[index[1]];
r4[index[2]] = a[index[0]];
for (int i = 0; i < r1.length; i++) {
System.out.print(r1[i] + " ");
}
System.out.println();
for (int i = 0; i < r1.length; i++) {
System.out.print(r2[i] + " ");
}
System.out.println();
for (int i = 0; i < r1.length; i++) {
System.out.print(r3[i] + " ");
}
System.out.println();
for (int i = 0; i < r1.length; i++) {
System.out.print(r4[i] + " ");
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Foo {
public static void main(String[] args) {
int[] n={1,2,3,4,5,6};
print(n);
change(n);
System.out.println();
print(n);
}
public static void change(int[] n) {
if (n == null || n.length < 2)
return;
int index = n.length / 2 + n.length % 2;
for (int i = 0; i < n.length / 2; i++, index++) {
int temp = n[i];
n[i] = n[index];
n[index] = temp;
}
}
public static void print(int[] n){
for(int i=0;i<n.length;i++){
System.out.print(n[i]+" ");
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询