Java的:操纵数组 [英] Java: Manipulating an Array
问题描述
我知道如何排序的阵列
按升序和降序排列,但我试图创建一个特定的模式。例如,我有一个随机的阵列
。我将如何解决这阵列
的模式呢? 最小的,最大的,第二小,第二大,THRID最小,第三大......等任何想法?
INT [] =模式{8,6,1,2,3,80,56};
//这是启动
公共类的Test2 { 公共静态无效的主要(字串[] args){
INT []数组= {1,4,2,6,9,3,65,77,33,22};
的for(int i = 0; I< array.length,我++){
System.out.print(+阵列[我]);
}
wackySort(数组); } //这个排序数组
公共静态无效wackySort(INT [] NUMS){
INT标志= 0;
INT温度= 0;
INT TEMP2 = 0;
的for(int i = 0; I< nums.length;我++){
对于(INT J = 0; J< nums.length -1; J ++){
如果(NUMS [J]> NUMS [J + 1]){
TEMP = NUMS [J]。
NUMS [J] = NUMS [J + 1];
NUMS [J + 1] =温度; }
}
}
的System.out.println();
INT firstPointer = 0;
INT secondPointer = nums.length -1;
INT [] = newarray新INT [nums.length]
的for(int i = 0; I< nums.length;我+ = 2){
newarray [I] = NUMS [firstPointer ++];
newarray [I] = NUMS [secondPointer--]
}
的for(int i = 0; I< newarray.length;我++){
System.out.print(+ newarray [I]);
}
}
}
好像功课。这样一个简单的提示
第1步:升序排序数组
{1,2,3,6,8,56,80};
第2步:分配同样大小的新数组
第3步通过第一阵列,两个反向迭代。在第一个点,最后另一点。现在新的阵列中分配第一计数器的数据,然后由一个增加的计数器。下一页最后分配计数器的数据和一个减少它。
INT firstPointer = 0;
INT secondPointer = nums.length - 1;
INT [] = newarray新INT [nums.length]
INT I = 0;
对于(I = 0; I&下; nums.length-1; I + = 2){
newarray [I] = NUMS [firstPointer ++];
newarray第[i + 1] = NUMS [secondPointer--];
}
如果(ⅰ&下; nums.length-1)
newarray [I] = NUMS [firstPointer ++];
I understand how to sort an array
by ascending and descending order, but there is a specific pattern I'm trying to create. For example, I have an array
in a random order. How would I sort this array
in the pattern? "smallest, largest, second smallest, second largest, thrid smallest, third largest..." etc. Any ideas?
int[] pattern = {8, 6, 1, 2, 3, 80, 56};
//This is the start
public class Test2 {
public static void main(String[] args) {
int[] array = {1,4,2,6,9,3,65,77,33,22};
for (int i = 0; i < array.length; i++) {
System.out.print(" " + array[i]);
}
wackySort(array);
}
//This sorts the array
public static void wackySort(int[] nums) {
int sign = 0;
int temp = 0;
int temp2 = 0;
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums.length -1; j++) {
if (nums[j] > nums[j+1]) {
temp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = temp;
}
}
}
System.out.println();
int firstPointer = 0;
int secondPointer = nums.length -1;
int[] newarray = new int[nums.length];
for (int i = 0; i < nums.length; i+=2) {
newarray[i] = nums[firstPointer++];
newarray[i] = nums[secondPointer--];
}
for (int i = 0; i < newarray.length; i++) {
System.out.print(" " + newarray[i]);
}
}
}
Seems like homework. so a easy hint
step 1: sort the array in ascending order
{1, 2, 3, 6, 8, 56, 80};
step 2: allocate a new array of same size
step 3 iterate through the first array, two counter. one points at first and another points at last. now in the new array assign the data of first counter then increase the counter by one. Next assign the data of last counter and decrease it by one.
int firstPointer = 0;
int secondPointer = nums.length - 1;
int[] newarray = new int[nums.length];
int i = 0;
for (i = 0; i < nums.length-1; i += 2) {
newarray[i] = nums[firstPointer++];
newarray[i+1] = nums[secondPointer--];
}
if(i<nums.length-1)
newarray[i] = nums[firstPointer++];
这篇关于Java的:操纵数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!