创建Java数组时使用尖括号和方括号方法的区别 [英] Difference between using angle and square bracket methods in creation of Java Arrays
问题描述
我最近刚开始使用Java,并且已经开始使用Arrays.据我所知,有两种创建数组的方法.
I've just recently started with Java and have gotten to Arrays. from what I can tell there are two ways of creating Arrays.
第一种方法对我来说最有意义,它来自python背景.
The first method makes the most sense to me coming from a python background.
type[] ArrayName;
即
int[] agesOfParticipants;
但是,许多在线资源使用另一种创建数组的方法.
However a lot of resources online use a different method of creating arrays.
ArrayList<ArrayType> Name = new ArrayList<ArrayType>;
这不仅有所不同,而且与我可以说的术语ArrayList根据情况至少是部分可互换的.例如,在此响应中,ArrayList被先前声明的类A取代.
not only is this different but from what I can tell the term ArrayList is at least partially interchangeable depending on circumstance. For instance in this response ArrayList is replaced by class A, which is declared earlier.
A<String> obj=new A<String>();
对不起,如果这一切都是基本的东西,但是我找不到真正能够将两者区分开的地方.
Sorry if this is all basic stuff, but I can't find anywhere that really distinguishes between the two.
推荐答案
在Java对象中,使用 new
关键字
In java objects are created using new
keyword
创建新的大小为
10
的Integer
数组,该数组由方括号[]
creating new
Integer
array with size10
, array consists of square brackets[]
Integer[] array = new Integer[10];
System.out.println(Arrays.toString(array)); // print array values `[..]`
创建值为10的
Integer
对象
Integer object = new Integer(10);
System.out.println(object); // print object value 10
创建仅包含
Integer
值
List<Integer> list = new ArrayList<>();
list.add(object);
System.out.println(object); // prints list with values [10]
尖括号
<>
是泛型,用于定义同类对象(例如,仅整数列表)
Angular brackets
<>
are Generics, that are used to define homogeneous type of objects (for example list of Integers only)
这篇关于创建Java数组时使用尖括号和方括号方法的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!