与第一个和最后一个交替打印数组(Java)

时间:2022-10-16 12:01:11

I have an assignment where I have to print a double array containing these numbers:

我有一个任务,我必须打印一个包含这些数字的双数组:

1, 2, 3, 4, 5, 6, 7

1,2,3,4,5,6,7

like this

1, 7, 2, 6, 3, 5, 4

1,7,2,6,3,5,4

So the first and last should be alternating from the beginning of the array and the ending of the array.

所以第一个和最后一个应该从数组的开头和数组的结尾交替。

I understand that the output can be confusing, but whats happening is that the first element should print (1), then the last element(7), then it must go back to the beginning and print the following element (3), then go back to the end and print the following element in the end (6) etc.

我理解输出可能令人困惑,但最新发生的是第一个元素应该打印(1),然后是最后一个元素(7),然后它必须返回到开头并打印下面的元素(3),然后去回到最后并在最后打印以下元素(6)等。

Here is my code so far:

这是我到目前为止的代码:

    public static void print(double [] a){
    int l = a.length-1;
    for(int i = 0; i < a.length; i++){
      if(a[i] == a[l]){
        System.out.print(a[i]);
        break;
      }
      System.out.print(a[i]+", "+a[l--]+", ");
    }
  }

The code works but only if the size of the array is odd. I want to make sure this code works will all types of sized arrays.. Please help

代码可以工作,但前提是数组的大小是奇数。我想确保此代码适用于所有类型的大小数组..请帮忙

3 个解决方案

#1


2  

The rules here are fairly simple:

这里的规则相当简单:

  • Print the first element.
  • 打印第一个元素。

  • Print the last element.
  • 打印最后一个元素。

  • Do not print the same element twice.
  • 不要两次打印相同的元素。

In terms of the array, you treat this as a bite-sized problem.

就数组而言,您将此视为一个一口大小的问题。

  • Print the first index.
  • 打印第一个索引。

  • Print the last index.
  • 打印最后一个索引。

  • Increment the pointer to the first index.
  • 将指针递增到第一个索引。

  • Decrement the pointer to the last index.
  • 将指针减少到最后一个索引。

  • If the two points are the same, break - you're done.
  • 如果这两点是相同的,那就打破 - 你已经完成了。

In terms of code, that would look like this. Note that in Java, you are permitted to declare multiple variables in your loop and use them in your expression later on.

在代码方面,这看起来像这样。请注意,在Java中,您可以在循环中声明多个变量,并在稍后的表达式中使用它们。

int[] values = {1, 2, 3, 2, 1};
// i shouldn't advance beyond the first half of the list.
// j shouldn't advance beyond the second half of the list.
for(int i = 0, j = values.length - 1; i <= values.length/2 && j >= values.length/2; i++, j--) {
    // i and j are at the same point - just print it and call it good.
    if(i == j) {
        System.out.print(values[i]);
        break;
    } else {
        System.out.print(values[i] + " ");
        System.out.print(values[j] + " ");
    }
}

#2


1  

A. Try i <= l - it will take care of the even/odd lengths

A.尝试i <= l - 它将处理偶数/奇数长度

B. Compare indicies and not the values, you need them till the middle of the array i>=a.length/2

B.比较指标而不是值,你需要它们直到数组的中间i> = a.length / 2

public static void print(double [] a) {
    int l = a.length - 1;
    for (int i = 0; i <= l; i++, l--) {
        if (i>=a.length/2) {
            System.out.print(a[i]);
            break;
        }
        System.out.print(a[i] + ", " + a[l] + ", ");
    }
}

#3


0  

    public static void print(double [] a) { 
 int l = a.length-1;  
for(int i = 0; i < a.length; i++){ 
 if(i ==l){  
System.out.print(a[i]); 
break;
    }  else if (i>l ){ 
break; 
} 
System.out.print(a[i]+", "+a[l--]+", "); 
    }

Please try this .. it would helpful

请试试这个..这会有所帮助

#1


2  

The rules here are fairly simple:

这里的规则相当简单:

  • Print the first element.
  • 打印第一个元素。

  • Print the last element.
  • 打印最后一个元素。

  • Do not print the same element twice.
  • 不要两次打印相同的元素。

In terms of the array, you treat this as a bite-sized problem.

就数组而言,您将此视为一个一口大小的问题。

  • Print the first index.
  • 打印第一个索引。

  • Print the last index.
  • 打印最后一个索引。

  • Increment the pointer to the first index.
  • 将指针递增到第一个索引。

  • Decrement the pointer to the last index.
  • 将指针减少到最后一个索引。

  • If the two points are the same, break - you're done.
  • 如果这两点是相同的,那就打破 - 你已经完成了。

In terms of code, that would look like this. Note that in Java, you are permitted to declare multiple variables in your loop and use them in your expression later on.

在代码方面,这看起来像这样。请注意,在Java中,您可以在循环中声明多个变量,并在稍后的表达式中使用它们。

int[] values = {1, 2, 3, 2, 1};
// i shouldn't advance beyond the first half of the list.
// j shouldn't advance beyond the second half of the list.
for(int i = 0, j = values.length - 1; i <= values.length/2 && j >= values.length/2; i++, j--) {
    // i and j are at the same point - just print it and call it good.
    if(i == j) {
        System.out.print(values[i]);
        break;
    } else {
        System.out.print(values[i] + " ");
        System.out.print(values[j] + " ");
    }
}

#2


1  

A. Try i <= l - it will take care of the even/odd lengths

A.尝试i <= l - 它将处理偶数/奇数长度

B. Compare indicies and not the values, you need them till the middle of the array i>=a.length/2

B.比较指标而不是值,你需要它们直到数组的中间i> = a.length / 2

public static void print(double [] a) {
    int l = a.length - 1;
    for (int i = 0; i <= l; i++, l--) {
        if (i>=a.length/2) {
            System.out.print(a[i]);
            break;
        }
        System.out.print(a[i] + ", " + a[l] + ", ");
    }
}

#3


0  

    public static void print(double [] a) { 
 int l = a.length-1;  
for(int i = 0; i < a.length; i++){ 
 if(i ==l){  
System.out.print(a[i]); 
break;
    }  else if (i>l ){ 
break; 
} 
System.out.print(a[i]+", "+a[l--]+", "); 
    }

Please try this .. it would helpful

请试试这个..这会有所帮助