【问题标题】:How to print [ ] in an array (Java)如何在数组中打印 [ ] (Java)
【发布时间】:2018-11-14 17:13:45
【问题描述】:

我想从用户那里获取 10 个数字并将它们输入到一个新数组中。如何将 [ ] 放入我的输出中?

import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;

public class Main {
public static void main(String[] args) {
    Scanner scnr = new Scanner(System.in);
    int num = 10;
    List<Integer> inputList = new ArrayList<>(num);
    while (num-- > 0) {
        inputList.add(scnr.nextInt());
    }
    List<Integer> goofyArray = new ArrayList<>(inputList.size());
    for (int i = inputList.size() - 1; i >= 0; i--) {
        if(inputList.get(i) % 7 == 0){continue;}
        if(inputList.get(i) < 0){
            goofyArray.add((inputList.get(i) * -1) * 2);
        } else {
            goofyArray.add(inputList.get(i));
        }
    }
    for (int number : goofyArray) {
        System.out.print( number + " ");
    }
  }
}

【问题讨论】:

  • 您不能从数组中删除元素。一个解决方案是建立一个新的阵列。或者——甚至更好——使用List
  • 用于处理负数 - 格式化代码 - 看起来 if (goofyArray[i] &lt; 0) 嵌套在 if ( goofyArray[i] % 7 == 0) 中。
  • 我错过了什么吗?为什么没有人回答这个问题:“我怎样才能将 [ ] 放入我的输出中?”你想要像“array[1] = 100”这样的东西吗?还是“[100 200 300]”?如果是这样,那么您必须通过连接(如"[" + i + "] = " + array[i])或格式化String.format(...) 来构建字符串
  • 查看我的更新答案:)
  • 将 [ ] 放入我的输出是什么意思?.

标签: java arrays reverse


【解决方案1】:

只使用静态数组,除了 Scanner 没有额外的导入

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        int NUM_OF_INPUTS = 10;
        int [ ] inputArray = new int [NUM_OF_INPUTS]; 
       for (int index = 0; index < NUM_OF_INPUTS; index++){
        inputArray[index] = scnr.nextInt(); 
       }

        int[] goofyArray = new int[inputArray.length];
        int[] arrayWithoutSevensAndReversed; // make a new array because static arrays don't have a remove
        int multiplesOfSeven = 0;

        // for loop 1: reverse and check how many multiples of 7
        for (int i = 0; i < goofyArray.length; i++) {
            goofyArray[i] = inputArray[goofyArray.length - 1 - i]; // Reverse the array.
            if (goofyArray[i] % 7 == 0)
                multiplesOfSeven++;
        }
        // for loop 2: use the new array with that will not contains multiples of seven the array
        arrayWithoutSevensAndReversed = new int[goofyArray.length - multiplesOfSeven];
        for (int x = 0, y = 0; x < goofyArray.length; x++) {
            if (goofyArray[x] % 7 != 0) { // is not multiple of 7, then add it to the array! 
                arrayWithoutSevensAndReversed[y] = goofyArray[x];
                if (arrayWithoutSevensAndReversed[y] < 0) { // make it positive then double! 
                    arrayWithoutSevensAndReversed[y] = arrayWithoutSevensAndReversed[y] * -1 * 2;
                }
                y++;
            }
        }

        for (int number : arrayWithoutSevensAndReversed) {
            System.out.print(number + " ");
        }
    }
}

【讨论】:

    【解决方案2】:

    如何将 [ ] 放入我的输出中?

    使用 util 包中的 Arrays.toString(int array[])

    System.out.println(Arrays.toString(goofyArray));
    

    【讨论】:

      【解决方案3】:

      不确定将 [ ] 放入我的输出是什么意思。试试这个:

      import java.util.Scanner;
      import java.util.List;
      import java.util.ArrayList;
      
      public class Main {
          public static void main(String[] args) {
              Scanner scnr = new Scanner(System.in);
              int num = 10;
              List<Integer> inputList = new ArrayList<>(num);
              while (num-- > 0) {
                  inputList.add(scnr.nextInt());
              }
              List<Integer> goofyArray = new ArrayList<>(inputList.size());
              //Reverse the array.
              for (int i = inputList.size() - 1; i >= 0; i--) {
                  if(inputList.get(i) % 7 == 0){continue;} //doesn't add numbers that are multiples of 7.
                  if(inputList.get(i) < 0){
                      goofyArray.add((inputList.get(i) * -1) * 2); // Change any numbers that are negative to be positive and twice their value.
                  } else {
                      goofyArray.add(inputList.get(i));
                  }
              }
              int[] output = new int[goofyArray.size()];
              output = goofyArray.toArray(output);
              for (int number : output) {
                  System.out.print( number + " ");
              }
          }
      }
      

      【讨论】:

        【解决方案4】:

        据我了解,您希望将数组打印为输出。

        你所要做的就是代替这个:

        for (int number : goofyArray) {
            System.out.print( number + " ");
        }
        

        这样做:

        System.out.print(goofyArray.toString());
        

        【讨论】:

          【解决方案5】:

          要从数组中删除元素,您可以使用 org.apache.commons.lang 包中的 ArrayUtils.remove(int array[], int index)

          int num[]=new int[] {10,20,30,40,50};
          num = ArrayUtils.remove(num, 2); 
          

          【讨论】:

            【解决方案6】:

            如果数组中有任何 42,我希望我的代码删除所有其他数字并简单地使 goofyArray 包含 1 个元素 42。在此示例中,我们有两个 42,其余是其他数字。因此它将删除除 42 之外的所有其他数字。

            public static void main(String[] args) {
                    int[] inputArray = { 5, -2, 42, 45, -6, 8, 42, -9, 10, 7 };
                    int[] tempArray = new int[inputArray.length];
                    int count=0;
                    for (int i = 0; i < inputArray.length; i++) {
                        if(inputArray[i]==42) {
                            tempArray[count]=42;
                            count++;
                        }
                    }
                    int[] goofArray=Arrays.copyOf(tempArray, count);
                    for(int i=0;i<goofArray.length;i++)
                        System.out.print(goofArray[i]+"  ");
            
                }
            

            在编辑您的问题之前:您想反转一个数组。如果数组包含任何负数,则首先使其为正数,然后两次(-9 将变为 18)它。如果数组包含可被 7 整除的元素,则将其删除。此示例将满足您的要求。

            public static void main(String[] args) {
                    int[] inputArray = { 5, -2, 28, 45, -6, 8, 42, -9, 10, 7 };
                    int length = inputArray.length;
                    int[] goofyArray = new int[length];
                    int count=0;
                    for (int i = 0; i < goofyArray.length; i++) {
                        if (inputArray[length - 1 - i] % 7 != 0) {
                            goofyArray[count] = inputArray[length - 1 - i]; // Reverse the array.
                            if (goofyArray[count] < 0) {
                                goofyArray[count] = Math.abs(goofyArray[count]) * 2;
                            }
                            count++;
                        }
                    }
                    int[] goofArray2=Arrays.copyOf(goofyArray, count);
                    for(int i=0;i<goofArray2.length;i++)
                        System.out.print(goofArray2[i]+"  ");
                }
            

            【讨论】:

              【解决方案7】:

              与其将其称为删除,不如将第 i+1 项移至第 i 项

                  if ( goofyArray[i] % 7 == 0){
              for(int x=i;x<goofyArray.length-1;x++){
              goofyArray[x]=goofyArray[x+1]; 
              }
              

              这里第 i 个词被删除了。

              【讨论】:

              • 线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: Main.main(Main.java:27) 的 10
              猜你喜欢
              • 1970-01-01
              • 2020-10-25
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-11-13
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多