java数组运算符_在java中创建数组时,新运算符会做什么?(What does the new operator do when creating an array in java?)...

在java中创建数组时,新运算符会做什么?(What does the new operator do when creating an array in java?)

当我们创建一个类型的对象时,新运算符在运行时分配内存。

myclass obj1 = new myclass();

这里myclass()定义了myclass的构造器

int arr1[4] = new int[];

new分配内存,但是, int[]在这里做什么?

When we create a object of a classtype the new operator allocates the memory at the run time.

Say

myclass obj1 = new myclass();

here the myclass() defines a constructur of myclass

but

int arr1[4] = new int[];

new allocates the memory but, what the int[] does here?

原文:https://stackoverflow.com/questions/3952370

更新时间:2019-11-13 09:32

最满意答案

你的代码:

int arr1[4] = new int[];

不会编译。 它应该是:

int arr1[] = new int[4];

在数组名称之前放置[]被认为是很好的做法,所以你应该这样做:

int[] arr1 = new int[4];

通常,数组创建为:

type[] arrayName = new type[size];

上面的[size]部分指定了要分配的数组的大小。

为什么我们在创建数组时使用new ?

因为Java中的数组是对象。 上面示例中的数组arrayName的名称不是实际的数组,而只是一个引用。 new运算符在堆上创建数组,并返回对新创建的数组对象的引用,然后将其分配给arrayName 。

Your code:

int arr1[4] = new int[];

will not compile. It should be:

int arr1[] = new int[4];

putting [] before the array name is considered good practice, so you should do:

int[] arr1 = new int[4];

in general an array is created as:

type[] arrayName = new type[size];

The [size] part above specifies the size of the array to be allocated.

And why do we use new while creating an array?

Because arrays in Java are objects. The name of the array arrayName in above example is not the actual array, but just a reference. The new operator creates the array on the heap and returns the reference to the newly created array object which is then assigned to arrayName.

2010-10-17

相关问答

使用op4j : Op.onListFor(a,b,c).get().contains(value);

使用相同的方法,你可以创建一个帮助类Is在一个方法in : class Is {

private T value;

public Is( T value ) { this.value = value; }

public boolean in( T... set ) {

for( T item : set ) {

if(

...

不 。 但它很容易编写,考虑使用自定义方法的enum : public enum Operator {

EQUAL("=="),

NOT_EQUAL("<>"),

GREATER_THAN(">"),

GREATER_THAN_OR_EQUAL(">="),

LESS_THAN("

LESS_THAN_OR_EQUAL("<=");

private final String representation;

private

...

你的代码: int arr1[4] = new int[];

不会编译。 它应该是: int arr1[] = new int[4];

在数组名称之前放置[]被认为是很好的做法,所以你应该这样做: int[] arr1 = new int[4];

通常,数组创建为: type[] arrayName = new type[size];

上面的[size]部分指定了要分配的数组的大小。 为什么我们在创建数组时使用new ? 因为Java中的数组是对象。 上面示例中的数组arrayName的名

...

不,Java不支持用户定义的运算符重载。 接近“自定义”运算符重载的Java的唯一方面是处理字符串的+,这会导致编译时使用StringBuilder / StringBuffer连接常量或执行时间连接。 您不能以相同的方式定义自己的操作符。 对于支持操作符重载的类Java(和基于JVM)的语言,您可以查看Groovy 。 或者,您可能会发现运行Java编译器插件解决方案 。 No, Java doesn't support user-defined operator overloading. Th

...

Java 确实有一个逻辑XOR运算符 ,它是^ (如在a ^ b )。 除此之外,您无法在Java中定义新的运算符。 编辑:这里有一个例子: public static void main(String[] args) {

boolean[] all = { false, true };

for (boolean a : all) {

for (boolean b: all) {

boolean c = a ^ b;

...

稍后是正确的new Class[10]将为内存中的10个对象创建占位符,您需要在其中显式放置对象。 Later is correct new Class[10] will create a placeholder for 10 objects in memory and you need to put objects explicitly in them.

没有operator[][] 。 如果你想提供那些语义,你需要重载operator[]这样它就会返回另一个同时重载operator[]对象。 您的案例可以使用向量向量来解决: #include

#include

#include

struct Block

{

int value = 0;

};

class Set

{

std::vector<:vector> > grid;

public:

...

你是绝对正确的 - @Autowired字段将连接到一个@Configurable注释类,即使在Spring容器之外,假设你有一个AspectJ基础设施。 你已经注意到了很好的捕获, @Value字段由Spring bean后处理器(AutowiredAnnotationBeanPostProcessor)处理,它解析了@Value注释的字段。 它不会对容器外部实例化的对象起作用 - 所以简而言之,@ Autowired字段应该@Value ,但@Value属性不会。 You are absolu

...

下面的例子你可以像这样为“$ cond”运算符创建数组: ArrayList gtA = new ArrayList();

gtA.add("$Sentiment");

gtA.add(0);

ArrayList condArray = new ArrayList();

condArray.add(new BasicDBObject("$gt", gtA));

condArray.add(1);

condArray.add(0);

BasicDBObject fullCond = new Basi

...

按位移位运算符在以下行中执行什么操作 为了反转数组,你只需要迭代直到它的长度的一半。 这就是位移操作员正在做的事情。 位移1会截断最后一位,相当于除以2。 length >> 1 == length / 2

如果没有按位移位运算符,则输出不会反转 如果迭代到length ,然后到length / 2 ,则每个元素将与末尾的相应元素交换。 但是,从那里继续前进将开始交换元素,元素朝左。 因此,最终的数组将与原始数组相同。 考虑这个简单的例子: arr = [1, 2, 3, 4, 5] 从索引0

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值