关于Java中数组赋值的有趣事实

先决条件: Java中的数组

null

在使用数组时,我们必须完成三项任务,即声明、创建、初始化或赋值。 数组声明:

int[] arr;

创建阵列:

// Here we create an array of size 3
int[] arr = new int[3];

数组的初始化:

arr[0] = 1;
arr[1] = 2;
arr[3] = 3;

int intArray[];    // declaring array
intArray = new int[20];  // allocating memory to array

将元素分配给数组时的一些重要事实:

  1. 对于基本数据类型: 对于基元类型数组,作为数组元素,我们可以提供 隐式提升为声明的类型数组 .除此之外,如果我们试图使用任何其他数据类型,那么我们将得到编译时错误,表示可能会丢失精度。

    // Java program to illustrate the concept of array
    // element assignments on int type array
    public class Test {
    public static void main(String[] args)
    {
    int [] arr = new int [ 3 ];
    arr[ 0 ] = 1 ;
    arr[ 1 ] = 'a' ;
    byte b = 10 ;
    arr[ 2 ] = b;
    System.out.println(arr[ 0 ] + arr[ 1 ] + arr[ 2 ]);
    }
    }

    
    

    输出:

    108
    

    // Java program to illustrate the concept of array
    // element assignments on int type array
    public class Test {
    public static void main(String[] args)
    {
    int [] arr = new int [ 3 ];
    // Assigning long value to int type.
    arr[ 0 ] = 10l;
    arr[ 1 ] = 'a' ;
    byte b = 10 ;
    arr[ 2 ] = b;
    System.out.println(arr[ 0 ] + arr[ 1 ] + arr[ 2 ]);
    }
    }

    
    

    输出:

    possible loss of precision.
    

    // Java program to illustrate the concept of array
    // element assignments on char type array
    public class Test {
    public static void main(String[] args)
    {
    char [][] arr = new char [ 2 ][ 2 ];
    // Assigning long value to int type.
    arr[ 0 ][ 0 ] = 10l;
    arr[ 0 ][ 1 ] = 'a' ;
    char b = 10 ;
    arr[ 1 ][ 0 ] = b;
    // Assigning double value to char type
    arr[ 1 ][ 1 ] = 10.6 ;
    }
    }

    
    

    输出:

    error: incompatible types: possible lossy conversion from long to char
    error: incompatible types: possible lossy conversion from double to char
    
  2. 对象类型数组: 如果我们正在创建对象类型数组,那么该数组的元素可以是声明的类型对象,也可以是子类对象。

    // Java program to illustrate the concept of array
    // element assignments on Number type array
    public class Test {
    public static void main(String[] args)
    {
    Number[] num = new Number[ 2 ];
    num[ 0 ] = new Integer( 10 );
    num[ 1 ] = new Double( 20.5 );
    System.out.println(num[ 0 ]);
    System.out.println(num[ 1 ]);
    }
    }

    
    

    输出:

    10
    20.5
    

    // Java program to illustrate the concept of array
    // element assignments on Number type array
    public class Test {
    public static void main(String[] args)
    {
    Number[] num = new Number[ 3 ];
    num[ 0 ] = new Integer( 10 );
    num[ 1 ] = new Double( 20.5 );
    // Here String is not the child class of Number class.
    num[ 2 ] = new String(“GFG”);
    }
    }

    
    

    输出:

    Compile-time error(incompatible types)
    

    // Java program to illustrate the concept of array
    // element assignments on Number type array
    public class Test {
    public static void main(String[] args)
    {
    Number[][] arr = new Number[ 2 ][ 2 ];
    arr[ 0 ][ 0 ] = 10l;
    // Assigning char to Number type array
    arr[ 0 ][ 1 ] = 'a' ;
    byte b = 10 ;
    arr[ 1 ][ 0 ] = b;
    // Assigning String to Number type array
    arr[ 1 ][ 1 ] = "GEEKS" ;
    }
    }

    
    

    输出:

    error: incompatible types: char cannot be converted to Number
    error: incompatible types: String cannot be converted to Number
    
  3. 接口类型数组: 对于接口类型数组,我们可以指定元素作为其实现类对象。

    // Java program to illustrate the concept of array
    // element assignments on Interface type array
    public class Test {
    public static void main(String[] args)
    {
    Runnable[] run = new Runnable[ 2 ];
    // Thread class implements Runnable interface.
    run[ 0 ] = new Thread();
    run[ 1 ] = new Thread();
    }
    }

    
    

    // Java program to illustrate the concept of array
    // element assignments on Interface type array
    public class Test {
    public static void main(String[] args)
    {
    Runnable[] run = new Runnable[ 2 ];
    run[ 0 ] = new Thread();
    // String class does not implements Runnable interface.
    run[ 1 ] = new String(“GFG”);
    }
    }

    
    

    输出:

    Compile-time error(Incompatible types)
    

    说明: 在上面的程序中,我们给出了导致编译时错误的字符串类元素。因为我们知道字符串并没有实现可运行的接口。

参考: https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html

本文由 比沙尔·库马尔·杜比 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享