简体   繁体   English

Java中的一维数组

[英]One dimensional array in Java

When I run this code on cmd prompt with statement :当我在 cmd 提示符下使用语句运行此代码时:

java Test A

output is输出是

a
b
c

Wasn't it suppose to result in an error since dimension of args is 1 whereas dimension of x is 3 (args=x).是不是假设会导致错误,因为 args 的维度是 1 而 x 的维度是 3 (args=x)。

class Test
{
   public static void main(String args[])
   {
      String[] x={"a","b","c"};

      args=x;
      for(String i: x)
      {
       System.out.println(i);
      }
   }
}

好吧,java中的数组变量只是一个引用,所以如果你给它另一个字符串数组的引用,它会接受它,所以数组变量(args)接受的值范围是对内存中字符串数组引用,它是就像将整数的值从 1 更改为 3 一样,这没问题,因为它们都是有效的,并且在整数接受的范围内。

It will not result in an error because your object is not final and you are not changing the array object.它不会导致错误,因为您的对象不是最终的并且您没有更改数组对象。

here这里

double[] data = new double[5]{2, 4, 5, 6, 8} // data can change but the instance of the class cant change
double[] data = new double[7] // here you are changing the data but not the object i.e the created instance of the object does not change but the instance the data(the variable) is holding changes

I hope you got your answer我希望你得到你的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM