array的几点注意

The declaration of an array does not allocate any storage, it just announces the intention of creating an array. A significant difference to the way C/C++ declares an array is that no size is specified with the identifier. Thus the following will cause a compile time error

int num[5];

Note that arrays have a length field not a length()

method.When you start to use Strings you will use the

string,length method,as in s.length();With an array the

length is a field (or property) not a method.

Java arrays know how big they are, and the language provides protection from accidentally walking off the end of them.

Note that at no point do you need to specify the number of elements in the array. You might get exam questions that ask if the following is correct.

int k=new int[5] {0,1,2,3,4} //Wrong, will not compile!

The elements of arrays are always set to default values wherever the array is created

int bl[] = new int[5];    for (int i = 0;i<bl.length;i++)

                                            bl[i] = i;      

 和 int bl[] = {1,2,3,4,5};同样可以为数组正确赋值.但是,与String一样,用NEW是产伤一个新的对象,而用

  int bl[] = {1,2,3,4,5};初始化时候先查找是否有该值存在,如没有则产生新对象,如果有就引用该已有对象.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,非常感谢您的提问。multiprocessing.Array实际上是一个用于进程间共享数据的数组,它是multiprocessing模块下的一个类。下面是一些详细的用法介绍: 1. 初始化一个数组:可以通过multiprocessing.Array(typecode, size, lock=True)方法来初始化一个数组。其中,typecode指定数组元素的类型,size指定数组的大小,lock参数表示是否使用锁。 2. 访问数组元素:可以使用普通数组的访问方式来访问multiprocessing.Array对象中的元素,例如:array[index]。 3. 多进程共享:multiprocessing.Array对象与普通的数组不同之处在于,其可以在多个进程之间共享。在多进程共享时,需要注意以下几点: (1)在父进程中创建Array对象; (2)当子进程需要访问Array对象时,需要使用相同的typecode、size以及lock参数创建新的Array对象,并通过共享的方式将其传递给子进程; (3)在父进程和子进程之间共享数据时,需要加锁避免出现竞争条件。 4. 示例代码: ``` import multiprocessing # 在父进程中创建Array对象 arr = multiprocessing.Array('i', [1, 2, 3, 4, 5]) def f(arr): # 子进程中访问Array对象 for i in range(len(arr)): arr[i] += 1 if __name__ == '__main__': # 创建新的进程 p = multiprocessing.Process(target=f, args=(arr,)) p.start() p.join() # 输出数组中的元素 print(list(arr)) ``` 本示例中,我们首先在父进程中创建一个包含5个整型元素的Array对象arr。然后,我们创建了一个子进程,在子进程中遍历Array对象并将每个元素加1。最后在父进程中输出Array对象中的元素。可以看到,由于Array对象是多进程共享的,所以子进程操作完毕后,父进程中的Array对象也被修改了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值