java 我如何声明一个空的 int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29403872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:12:11  来源:igfitidea点击:

How can I declare an empty int

javaandroid-studioint

提问by Zablah

I want to declare an empty intlike you would do with a String: String userName ="";I need an empty int because I want to select a (random) picture from an array:

我想像int使用 String 一样声明一个空的: String userName ="";我需要一个空的 int 因为我想从一个数组中选择一个(随机)图片:

public int[] mBackground = {
       R.drawable.heart,
       R.drawable.cane,
       R.drawable.watch
};

This is the code I use to get a random picture from the array:

这是我用来从数组中获取随机图片的代码:

public int getBackground(){
int background = "";
    Random generate = new Random();
    int randomNumber = generate.nextInt(mBackground.length);

    background = mBackground[randomNumber];

    return background;
}

采纳答案by Fred

I don't understand your question. Why do you need to initialize it? You don't even need the variable.

我不明白你的问题。为什么需要初始化呢?你甚至不需要变量。

This code works as you want

此代码按您的需要工作

public int getBackground(){
    Random generate = new Random();
    int randomNumber = generate.nextInt(mBackground.length);
    return mBackground[randomNumber];
}

However, if you want to have a variable you can do

但是,如果你想要一个变量,你可以这样做

public int getBackground(){
    Random generate = new Random();
    int randomNumber = generate.nextInt(mBackground.length);
    int background = mBackground[randomNumber];
    return background;
}

回答by gknicker

If you must assign your int backgroundvariable a default value for some reason, -1is often workable. You cannot assign an intvariable to anything other than an integer value (""is a Stringso won't work).

如果int background出于某种原因必须为变量分配一个默认值,-1这通常是可行的。您不能将分配int变量不是一个整数值以外的任何(""String这样将无法正常工作)。

    int background = -1;
    // ...
    if (background < 0) {
        // handle invalid value, perhaps assign a default
    }

If you need your variable to point to "nothing", consider using the boxed type Integer, which you can initialize to null.

如果您需要变量指向“无”,请考虑使用盒装类型Integer,您可以将其初始化为null

    Integer background = null; // will throw NullPointerException if unboxed to int

Otherwise, don't declare the variable until you're ready to assign it. That way you don't have to give it a pointless initial value.

否则,在准备分配变量之前不要声明变量。这样你就不必给它一个毫无意义的初始值。

    int background = mBackground[randomNumber];

Or you could just return the value straight from your method instead of assigning it to an intermediate variable.

或者您可以直接从您的方法中返回值,而不是将其分配给中间变量。

    return mBackground[randomNumber];

You actually don't have to initialize a local variable at all when you declare it. This is perfectly acceptable.

当你声明一个局部变量时,你实际上根本不需要初始化它。这是完全可以接受的。

    int background;

However, you must assign a value before you can use it.

但是,您必须先分配一个值,然后才能使用它。

Here's the shortest statement of your method's intention.

这是您的方法意图的最短陈述。

public int getBackground(){
    return mBackground[new Random().nextInt(mBackground.length)];
}

回答by James S

There is no such thing as an empty int in the context you have given, an empty string is a String containing a sequence of 0 length. int is a primitive data type and can't take any value outside the parameters of either a signed or unsigned 32-bit integer unless you want to declare as an Integer boxed Object in which case you can declare it as null.

在您给出的上下文中没有空 int 之类的东西,空字符串是包含长度为 0 的序列的字符串。int 是一种原始数据类型,不能在有符号或无符号 32 位整数的参数之外取任何值,除非您想声明为整数盒装对象,在这种情况下,您可以将其声明为 null。

For a method as simple as selecting a random value from an array you may as well just use a one-liner.

对于从数组中选择随机值这样简单的方法,您也可以只使用单行。

public int getBackground(){
    return mBackground[new Random().nextInt(mBackground.length)];
}