public class ArrayGenerator {
  
  public static int[] generateArray(int size) {
    int[] array = new int[size];
    for (int i = 0; i < size; i++) {
      array[i] = 0;
    }
    return array;
  }
  
  public static void main(String[] args) {
    int[] newArray = generateArray(5);
    for (int i = 0; i < newArray.length; i++) {
      System.out.println(newArray[i]);
    }
  }
  
}

This Java code defines a method generateArray that takes an integer size as input and returns an integer array of that size, with all elements initialized to 0. The code then demonstrates how to use this method by creating a new array of size 5 and printing its elements.

Java Method to Generate Zero-Filled Integer Array

原文地址: https://www.cveoy.top/t/topic/lPz7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录