java - 无法在 Java 中的 for 循环中打印整数数组列表

标签 java arrays for-loop printing int

下面的代码片段。我尝试在 for 循环之外初始化变量,但仍然收到相同的错误。

我无法在 for 循环之外打印变量数组。

public class count_test01 {

    public static void main(String[] args) {

        int n = 2;
        countTest(n);
    }


    public static void countTest(int num){

        //int[] longCnt01_intArray01;

        int cnt01, cnt02;
        long longCnt01, longCnt02;

        longCnt01 = 1;

        // int holds 10 digits in length, MAX is 2,147,483,647.
        // long holds holds 19 digits in length, MAX is 9,223,372,036,854,775,807.
        for(int i = 1; i <= 11; i++) { //63; i++) { //(long i = 1; i <= 9000000000000000000L; i++) {            
            longCnt01 *= i; //2; //at 2, i can be <= 63.


            // Convert Integer Count to String.
            String longCnt01String = BigDecimal.valueOf(longCnt01).toPlainString(); 

            //Convert String to Integer Array.
            int[] longCnt01_intArray = new int[longCnt01String.length()];

            for (int i1 = 0; i1 <= longCnt01String.length()-1; i1++){
                longCnt01_intArray[i1] = longCnt01String.charAt(i1) - '0';
            }

            // Create new integer array, save a copy.
            int[] longCnt01_intArray01 = new int[longCnt01_intArray.length];
            for( int i2 = 0; i2 < longCnt01_intArray.length; i2++){

                longCnt01_intArray01[i2] = longCnt01_intArray[i2];
            }

            System.out.println("Count of i           : " + i + "\n");
            System.out.println("Count of longCnt01   : " + longCnt01 + "\n");
            System.out.println("Size of longcnt01    : " + Arrays.toString(longCnt01_intArray) + "\n\n");
        //System.out.println("Size of longCnt01    : " + Arrays.toString(longCnt01_intArray01) + "\n");
        }

        System.out.println("Size of longCnt01    : " + Arrays.toString(longCnt01_intArray01) + "\n");
        System.out.println("Count of longCnt01   : " + longCnt01 + "\n");
}

提示代码太多,但输入的文本细节不够。

最佳答案

好吧,我已经制定了一个可行的解决方案,告诉我如何去做。不过,我重构了很多代码,只是为了警告您,所以请询问您是否有任何问题或想知道如何根据您的需求进行调整,以防它与您的想法不太相符。

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Example {

  public static void main(String[] args) {
    int n = 2;
    countTest(n);
  }

  public static void countTest(int num) {
    int cnt01, cnt02;
    long longCnt01, longCnt02;

    //Make a scalable arraylist to hold all the copies
    List<Integer[]> copies = new ArrayList<>();

    longCnt01 = 1;

    for (int i = 1; i <= 11; i++) { 
      //Moved print here to keep it logically close to it's related component
      System.out.println("Count of i           : " + i + "\n");

      longCnt01 *= i;
      //Moved print here to keep it logically close to it's related component
      System.out.println("Count of longCnt01   : " + longCnt01 + "\n");

      // First thing, refactored the Integer array retrieval into it's own method 
      // and use Integer class instead to make use of a scalable ArrayList
      Integer[] longCnt01_intArray = getIntArrayFromCount(longCnt01);

      // Quick and easy way to make a copy then store in the arraylist for later use
      copies.add(longCnt01_intArray.clone());
    }

    //Use a loop to gro through all copies
    for(int i = 0; i < copies.size(); i++) {
      System.out.println("Size of Copy " + i + ": " + Arrays.toString(copies.get(i)) + "\n");
    }
    System.out.println("Count of longCnt01   : " + longCnt01 + "\n");
  }

  private static Integer[] getIntArrayFromCount(long count) {
    // Convert Integer Count to String.
    String longCnt01String = BigDecimal.valueOf(count).toPlainString();

    // Convert String to Integer Array.
    Integer[] longCnt01_intArray = new Integer[longCnt01String.length()];

    for (int i1 = 0; i1 < longCnt01String.length(); i1++) {
      longCnt01_intArray[i1] = longCnt01String.charAt(i1) - '0';
    }

    //Moved print here to keep it logically close to it's related component
    System.out.println("Size of longcnt01    : " + Arrays.toString(longCnt01_intArray) + "\n\n");

    return longCnt01_intArray;
  }
}

关于java - 无法在 Java 中的 for 循环中打印整数数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60933927/

相关文章:

java - RecyclerView 高度 wrap_content 在 NestedScrollView 中不起作用

java - 使用 Spring 解析和使用 HTTP Range header

java - 将 Box2D 实体旋转到鼠标输入点?

javascript - 如何使用递归将一个对象转换为另一个对象。 Javascript

php - 如果我用第 n 个索引在 PHP 中启动一个数组,它将占用多少内存?

java - native 分配失败时,Java 不会创建堆转储

c - 结构数组的排序成员

java - 输入验证使用数组和 for 循环检查重复项和格式

javascript - for-of 循​​环中的属性访问有什么作用,比如 `for (a.b of c)` ?

r - For 循环生成具有属性 (n - 1) * 16 的 url