Java中的最终数组 [英] final array in Java

查看:122
本文介绍了Java中的最终数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java中的以下代码使用最终 String 数组,并且毫无疑问。

 最终公共类Main {
public static final String [] CONSTANT_ARRAY = {I,can,never ,改变};

public static void main(String [] args){
for(int x = 0; x< CONSTANT_ARRAY.length; x ++){
System.out.print( CONSTANT_ARRAY [x] +);
}
}
}

它显示以下输出控制台。

 我永远无法改变






以下代码也毫无疑问。

  final public class Main {
public static final String [] CONSTANT_ARRAY = {I,can,never,change};

public static void main(String [] args){
// CONSTANT_ARRAY = {I,can,never,change}; //错误 - 无法分配给最终变量CONSTANT_ARRAY。
for(int x = 0; x< CONSTANT_ARRAY.length; x ++){
System.out.print(CONSTANT_ARRAY [x] +);
}
}
}

显然,注释行会导致指定的错误,因为我们试图重新分配类型 String 的声明的 final 数组。






以下代码如何。

  final public class Main {
public static final String [] CONSTANT_ARRAY = {I,can,never,change};

public static void main(String [] args){
CONSTANT_ARRAY [2] =always; //编译好。
for(int x = 0; x< CONSTANT_ARRAY.length; x ++){
System.out.print(CONSTANT_ARRAY [x] +);
}
}
}

并显示我总是可以改变意味着我们可以设法修改类型为 String <的 final 数组的值/ code>。我们能否以这种方式修改整个数组而不违反 final 的规则?

解决方案

final 会影响变量,它与您分配给它的对象无关。

  final String [] myArray = {hi,there}; 
myArray = anotherArray; //错误,你做不到。 myArray是最终的
myArray [0] =over; //完全没问题,final与它无关

编辑从评论中添加: 请注意,我说您要分配给它的对象。在Java中,数组是一个对象。同样的事情适用于任何其他对象:

  final List< String> myList = new ArrayList< String>():
myList = anotherList; //错误,你不能这样做
myList.add(你好!); //完全没问题


The following code in Java uses a final array of String and there is no question about it.

final public class Main {
  public static final String[] CONSTANT_ARRAY = {"I", "can", "never", "change"};

  public static void main(String[] args) {
    for (int x = 0; x < CONSTANT_ARRAY.length; x++) {
      System.out.print(CONSTANT_ARRAY[x] + " ");
    }
  }
}

It displays the following output on the console.

I can never change


The following code also goes without any question.

final public class Main {
  public static final String[] CONSTANT_ARRAY = {"I", "can", "never", "change"};

  public static void main(String[] args) {
    //CONSTANT_ARRAY={"I", "can", "never", "change"}; //Error - can not assign to final variable CONSTANT_ARRAY.
    for (int x = 0; x < CONSTANT_ARRAY.length; x++) {
      System.out.print(CONSTANT_ARRAY[x] + " ");
    }
  }
}

Obviously, the commented line causes the specified error because we are trying to reassign the declared final array of type String.


What about the following code.

final public class Main {
  public static final String[] CONSTANT_ARRAY = {"I", "can", "never", "change"};

  public static void main(String[] args) {
    CONSTANT_ARRAY[2] = "always";  //Compiles fine.
    for (int x = 0; x < CONSTANT_ARRAY.length; x++) {
      System.out.print(CONSTANT_ARRAY[x] + " ");
    }
  }
}

and it displays I can always change means that we could manage to modify the value of the final array of type String. Can we ever modify the entire array in this way without violating the rule of final?

解决方案

final in Java affects the variable, it has nothing to do with the object you are assigning to it.

final String[] myArray = { "hi", "there" };
myArray = anotherArray; // Error, you can't do that. myArray is final
myArray[0] = "over";  // perfectly fine, final has nothing to do with it

Edit to add from comments: Note that I said object you are assigning to it. In Java an array is an object. This same thing applies to any other object:

final List<String> myList = new ArrayList<String>():
myList = anotherList; // error, you can't do that
myList.add("Hi there!"); // perfectly fine. 

这篇关于Java中的最终数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆