Java 如何限制空值存储在数组中?

Java 如何限制空值存储在数组中?,java,arrays,Java,Arrays,不幸的是,我的导师不允许我在这个作业中使用列表 我想限制空值存储在数组中。我尝试使用if语句,但它不起作用 public static void main(String[] args) { System.out.println(messages(3, 10)); } public static String message(int n) { String message = null; if ((n % 3 != 0) && (n

不幸的是,我的导师不允许我在这个作业中使用列表

我想限制空值存储在数组中。我尝试使用if语句,但它不起作用

public static void main(String[] args) {

    System.out.println(messages(3, 10));
}

public static String message(int n) {

        String message = null;

        if ((n % 3 != 0) && (n % 5 != 0)){
            message = null;
        } else if((n % 3 == 0) && (n % 5 == 0)) {
            message = n + ": FizzBuzz";
        } else if (n % 3 == 0) {
            message = n + ": Fizz";
        } else if (n % 5 == 0) {
            message = n + ": Buzz";
        } 

        return message;
 }

public static String[] messages(int start, int end) throws IllegalArgumentException {
    if (end < start) {
        throw new IllegalArgumentException();

    } else {

    String[] msgArray = new String[end - start];

    int j = 0;

    for(int i = start; i < end; i++) {
                String theMsg = message(i);


 /* Where I need help */

 // I'm trying to use the if statement to restrict 
 //all null values from being stored in my msgArray */  

                if(theMsg != null) {



                msgArray[j] = theMsg;

                }
                j++;
        }

    System.out.println();

    for(String s: msgArray) {
        System.out.println(s);
    }

    return msgArray;
        }
    }
实际产量

 3: Fizz
 null
 5: Buzz
 6: Fizz
 null
 null
 9: Fizz

将j++语句放入if块中,然后重试

            if(theMsg != null) {



            msgArray[j] = "dan";
            j++;
            }

如果创建数组,则指定大小。然后创建数组,并用默认值填充。在本例中,对于
String
s,这是
null
(对于
int[]
s,这是
0
)。这是没有办法的。您必须从创建数组开始指定正确的长度,并且该长度不会更改。所以你有两个选择:

  • 在创建数组之前,以某种方式计算实际的非空值的数量,然后创建一个具有该大小的数组,而不是当前的计算。这涉及到你逻辑上的很多变化所以
  • 我建议您仅从当前数组创建一个新数组,并在放入该数组之前检查该值是否为
    null
  • 见下文:

    // inside your function
    // instead of returning msgArray, create a new array and modify it.
    
    // get the count of values that aren't null
    int nonNull = 0;
    for (int index = 0; index < msgArray.length; index++) {
        if (msgArray[index] != null) nonNull++;
    }
    
    // create the new array because you can't change the size of your other array
    String[] newMsgArray = new String[nonNull];
    
    // populate the array
    int newIndex = 0;
    for (int index = 0; index < msgArray.length; index++) {
        if (msgArray[index] != null) {
            newMsgArray[newIndex] = msgArray[index];
            newIndex++;
        }
    }
    
    return newMsgArray;
    
    //函数内部
    //创建一个新数组并修改它,而不是返回msgArray。
    //获取非空值的计数
    int-nonNull=0;
    对于(int index=0;index
    您正在分配一个大小为(结束-开始)的数组,该数组将用空元素初始化。然后它的一些索引将被覆盖。因此空元素仍然存在,并且无法删除(Java数组)。一个解决方案可能是将完整索引复制到另一个数组中,或者首先分配正确的数组大小。

    一个更优化的方法是使用
    List
    s。但是因为这是不允许的,所以你可以在你的代码中做这两个改变

  • 您可以在
    if
    块内移动
    j++
  • 要删除数组中所有剩余的
    null
    ,可以放置
    msgArray=Arrays.copyOfRange(msgArray,0,j)就在if语句之后
  • 输出

    3: Fizz
    5: Buzz
    6: Fizz
    9: Fizz
    [Ljava.lang.String;@6d06d69c
    
    代码

    import java.util.*;
    
    public class HelloWorld {
        public static void main(String[] args) {
            System.out.println(messages(3, 10));
        }
    
        public static String message(int n) {
            String message = null;
    
            if ((n % 3 != 0) && (n % 5 != 0)) {
                message = null;
            } else if ((n % 3 == 0) && (n % 5 == 0)) {
                message = n + ": FizzBuzz";
            } else if (n % 3 == 0) {
                message = n + ": Fizz";
            } else if (n % 5 == 0) {
                message = n + ": Buzz";
            }
    
            return message;
        }
    
        public static String[] messages(int start, int end) throws IllegalArgumentException {
            if (end < start) {
                throw new IllegalArgumentException();
            } else {
                String[] msgArray = new String[end - start];
                int j = 0;
    
                for (int i = start; i < end; i++) {
                    String theMsg = message(i);
    
    
                    /* Where I need help */
    
                    // I'm trying to use the if statement to restrict 
                    //all null values from being stored in my msgArray */  
    
                    if (theMsg != null) {
                        msgArray[j] = theMsg;
                        j++; //Change #1
                    }
                }
    
                msgArray = Arrays.copyOfRange(msgArray, 0, j); //Change #2
    
                System.out.println();
    
                for (String s: msgArray) {
                    System.out.println(s);
                }
    
                return msgArray;
            }
        }
    }
    
    import java.util.*;
    公共类HelloWorld{
    公共静态void main(字符串[]args){
    System.out.println(消息(3,10));
    }
    公共静态字符串消息(int n){
    字符串消息=null;
    如果((n%3!=0)和&(n%5!=0)){
    消息=null;
    }else如果((n%3==0)和&(n%5==0)){
    消息=n+“:嘶嘶作响”;
    }否则如果(n%3==0){
    消息=n+“:嘶嘶”;
    }否则如果(n%5==0){
    消息=n+“:嗡嗡声”;
    }
    返回消息;
    }
    公共静态字符串[]消息(int start,int end)引发IllegalArgumentException{
    如果(结束<开始){
    抛出新的IllegalArgumentException();
    }否则{
    字符串[]msgArray=新字符串[结束-开始];
    int j=0;
    for(int i=start;i
    不够好。这只会将
    null
    移动到输出中的另一个位置。如果不想打印
    null
    ,则在
    System.out.println
    周围放置
    If
    ,以防止打印
    null
    。嘿。我不希望它们存储在数组中。因此,当我使用for-each循环在下面打印msgaray时,我仍然会得到您在上面看到的“实际输出”。您的代码看起来可疑。你在哪里向加里小姐(msgArray[j])填海报?有时候很难说海报到底想要什么。只要看看您想要的输出,我可以想象一些人可能会认为他们必须避免在数组中存储空值——但要获得该输出,您不需要避免存储它们,只需要避免打印它们。人们可能会沉迷于某种解决问题的方法,而错过更好的方法。如果您需要一个没有空值的数组有不同的原因——或者该数组将用于其他用途,或者您的导师告诉您创建一个没有空值的数组——那么好吧,答案会告诉您如何创建。
    import java.util.*;
    
    public class HelloWorld {
        public static void main(String[] args) {
            System.out.println(messages(3, 10));
        }
    
        public static String message(int n) {
            String message = null;
    
            if ((n % 3 != 0) && (n % 5 != 0)) {
                message = null;
            } else if ((n % 3 == 0) && (n % 5 == 0)) {
                message = n + ": FizzBuzz";
            } else if (n % 3 == 0) {
                message = n + ": Fizz";
            } else if (n % 5 == 0) {
                message = n + ": Buzz";
            }
    
            return message;
        }
    
        public static String[] messages(int start, int end) throws IllegalArgumentException {
            if (end < start) {
                throw new IllegalArgumentException();
            } else {
                String[] msgArray = new String[end - start];
                int j = 0;
    
                for (int i = start; i < end; i++) {
                    String theMsg = message(i);
    
    
                    /* Where I need help */
    
                    // I'm trying to use the if statement to restrict 
                    //all null values from being stored in my msgArray */  
    
                    if (theMsg != null) {
                        msgArray[j] = theMsg;
                        j++; //Change #1
                    }
                }
    
                msgArray = Arrays.copyOfRange(msgArray, 0, j); //Change #2
    
                System.out.println();
    
                for (String s: msgArray) {
                    System.out.println(s);
                }
    
                return msgArray;
            }
        }
    }