java 用户定义的数组(和用户定义的数组大小)返回 [null, null, null, ...]

标签 java arrays null user-defined

这是我在这个网站上的第一个问题,我在 NetBeans 8.0.2 上运行它并尝试打印出我的用户定义的数组,但它一直返回空值。例如,如果你说有 2 名员工并输入他们的名字,它将返回 [null, null]

如何修复这个错误?我是新手。

import java.util.Scanner;
import java.text.DecimalFormat;
import java.util.Arrays;

class Tips_Calculation2 
{
public static void main(String[] args)
{
    Scanner scan = new Scanner(System.in);

    System.out.print("How many employees for the week?: ");
    int numberOfEmps = scan.nextInt();

    // counter for the if statement that should return all employees from the array
    int counter = numberOfEmps;

    int[] nOEarray = new int[numberOfEmps];


    System.out.println("\nEnter names of workers up to the entered amount (" + numberOfEmps + "):");


    for(int i = 1; i <= numberOfEmps; i++)
    {
        String nameCycler = scan.next();
        String[] namesArray = new String[i];

        if(counter == i)
        {
            System.out.println(Arrays.toString(namesArray));
        }
    }
}
}

忽略 import java.text.DecimalFormat,因为我计划稍后在我的代码中使用此导入。预先感谢任何善良/聪明的回复者。

最佳答案

首先,您从未将 nameCycler 放入数组中。其次,您每次迭代都会创建 namesArray ,我认为这是错误的。

关于java 用户定义的数组(和用户定义的数组大小)返回 [null, null, null, ...],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32197513/

相关文章:

java - 使用 Arrays.sort 对数组元素进行排序

ios - UIButton 从 Core Data 增加一个数组,从另一个类完成

java - Java中的字符串压缩

java - 是否可以在 Java 的不同位置创建完全相同的对象?

java - 在没有新方法的情况下通过 if 语句传递变量

python - 在没有并行化的情况下提高功能的性能

MySQL 列默认值 - 优点/缺点,我应该用于所有列吗?

java - 尺寸较大但全为空值的对象列表

c++ - 为什么对cout << g_System-> FileSystem()-> GetFileData (“test.txt”取消引用NULL的工作;在这个例子中?

java - 我如何从 JBoss 中的 EJB 调用 JNI 代码