Java Arrays Tutorial (2)

Two-dimenstional arrays have two or more columns of values, for example, the following code assigns values to someNumbers when it is created:

Int[][] someNumbers = {{8,9,10,11},

                                           {1,3,12,15},

                                           {5,9,44,99}};

The someNumbers array contains three rows and four columns.

Besides one- and two- dimensional arrays, Java also supports arrays with three, four, and more dimensions. The general term for arrays with more than one dimension is multidimensional arrays.

The methods in the Arrays class are static methods, which means you use them with the class name without instantiating an Arrays object. The Aarrays class is located in the java.util.* package, so you can use the statement import java.util.*; to access it.

In addition to the Arrays class, Java provides an ArrayList class which can be used to create containers that store lists of objects. The ArrayList class provides some advantages over the Arrays class. Specifically, an ArrayList is dynamically resizable, meaning that its size can change during program execution. This means that:

(1). You can add an item at any point in an ArrayList container, and the array size expands automatically to accommodate the new item.

(2). You can remove an item at any point in an AarrayList container, and the array size contracts automatically.

As in the following example:

ArrayList names = new ArrayList();

The default constructor creates an Aarraylist with a capacity of 10 items.

An AarrayList can be used to store any type of object reference. In fact, one ArrayList can store multiple types. However, this creates two drawbacks:

(1). You cannot use an ArrayList to store primitive types such as int, double, or char because those types are not references. If you want to work with primitive types, you can create an array or use the Arrays class, but you cannot use the ArrayList class.

(2). When you want to store ArrayList elements, you must cast them to the appropriate reference type before you can do so, or you must declare a reference type in the ArrayList declaration.

For example, if you want to declare a String to hold the first name in the names ArrayList, you must make statements such as the following:

String firstName;

firstName=(String)names.get(0);

The cast operator(String) converts the generic returned object from the get() method to a String. If you do not perform this case, you receive an error message indicating that you are using incompatible types.

You can eliminate the need to perform a cast with ArrayList objects by specifying the type that an ArrayList can hold. For example, you can declare an ArrayList of names as follows:

AarrayList<String> names = new ArrayList<String>();

Creating an ArrayList declaration with a specified type provides several advantages:

(1). You no longer have to use the cast operator when retrieving an item from the AarrayList.

(2). Java checks to make sure that only items of the appropriate type are added to the list.

(3). The compiler warning that indicates your program uses an unchecked or unsafe operation is eliminated.

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值