定义:
double[] myList; (最最常用定义方法)
数组的元素类型和数组的大小都是确定的
遍历数组:
方式一:for循环
// 打印所有数组元素
for (int i = 0; i < myList.length; i++) {
System.out.println(myList[i] + " ");
}
方式二:foreach 循环
// 打印所有数组元素
for (double element: myList) {
System.out.println(element);
}
判断数组包含一个元素:
此时使用ArrayList或者Vector容器中的contains方法。
使用举例,欢迎指正
// 验证返回值的字段完整
ArrayList expectFieldNamesList = new ArrayList<>();
String[] expectFieldNames = {
"time", "data", "total", "pn", "ps", "list", "clusterId", "count",
"question", "matchTypeValue", "source", "typeId",
"typeIdValue", "reviewStatus", "id", "version", "createDate"};
for (String str : expectFieldNames) {
expectFieldNamesList.add(str);
}
Iterator realFieldNames = res.fieldNames();
int realFieldsCount = 0;
while (realFieldNames.hasNext()) {
realFieldsCount += 1;
Assert.assertTrue(expectFieldNamesList.contains(realFieldNames.next()), "返回字段 " + realFieldNames.next() +
"不符合预期");
}
Assert.assertEquals(realFieldsCount, expectFieldNamesList.size(), "接口clusterList返回的字段不完整");
判断数组是否相等:
Arrays.equals(ary, ary1));
扩展
1、Vector是线程安全的,ArrayList不是线程安全的。大多数情况下不使用Vector,因为线程安全需要更大的系统开销。
2、ArrayList在底层数组不够用时在原来的基础上扩展0.5倍,Vector是扩展1倍。
参考: