[英]Passing an ArrayList between methods
In my continuing education on Arrays and ArrayLists I'm trying to smarten up my code by passing an ArrayList from one method to another. 在继续学习Arrays和ArrayLists的过程中,我试图通过将ArrayList从一种方法传递到另一种方法来优化代码。 Here is my code:
这是我的代码:
public void exampleArrayList () {
ArrayList<String> al = new ArrayList<String>();
al.add("AZ");
al.add("BY");
al.add("CX");
al.add("DW");
al.add("EV");
al.add("FU");
al.add("GT");
display(al);
}
public void display(ArrayList al) {
System.out.println("Index of 'AZ': " + al.indexOf("AZ"));
System.out.println("Index of 'FU': " + al.indexOf("FU"));
System.out.println("Index of 'AA': " + al.indexOf("AA"));
System.out.println("Index of 'CX': " + al.indexOf("CX"));
// for (String row : al)
// System.out.println("Value at Index " + al.indexOf(row) +
// " is " + al.get(al.indexOf(row)));
for(int i = 0; i < al.size(); i++)
System.out.println("Value at Index " + al.indexOf(i) +
" is " + al.get(al.indexOf(i)));
}
In the display method works with both for statements commented out. 在display方法中,两个for语句都被注释掉了。 The for statement that is currently commented out doesn't work because row is looking for a String but get's assigned an object even although array al is a string.
当前注释掉的for语句不起作用,因为row正在寻找一个String,但是即使数组al是一个字符串,也为get分配了一个对象。 Do I need to cast al into a string or something?
我是否需要将al强制转换为字符串或其他内容? This is not the case when I run the for loop when the loop is in the same method that created the ArrayList in and I don't understand the difference.
当循环在创建ArrayList的相同方法中运行for循环时,情况并非如此,我不了解其中的区别。
The second for statement that isn't commented out causes a crash giving me the following runtime error: 第二个未注释的for语句会导致崩溃,并显示以下运行时错误:
java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1
I tried changing the i < al.size()
to a hard coded number and it still failed and I don't know why. 我尝试将
i < al.size()
更改为硬编码数字,但仍然失败,我不知道为什么。
1) You need to pass it as an ArrayList<String>
: 1)您需要将其作为
ArrayList<String>
传递:
public void display(ArrayList<String> al) {
^^^^^^^^
2) You're searching for the integers in the list. 2)您正在搜索列表中的整数。 The list doesn't contain any integers, so
indexOf
returns -1. 该列表不包含任何整数,因此
indexOf
返回-1。 Then you call al.get(-1)
where -1 is obviously out of bounds. 然后调用
al.get(-1)
,其中-1显然超出范围。 I'm not sure what you meant to do here. 我不确定您打算在这里做什么。
You are using indexOf()
, which, given an int
, will search for that int
and return its index if the list contains it. 您正在使用
indexOf()
,给定一个int
,它将搜索该int
并在列表包含索引的情况下返回其索引。 As this isn't the case - it is a List<String>
- you get index out of bounds because you are trying to retrieve an element at index -1. 因为不是这种情况-它是
List<String>
-索引超出范围,因为您试图检索索引-1处的元素。 -1 is returned from indexOf()
if the element can't be found, which it can't. 如果找不到元素,则从
indexOf()
返回-1。
This is why you shouldn't use raw types. 这就是为什么您不应该使用原始类型的原因。 Use
get()
and a List<String>
as your parameter (no need to make it specifically ArrayList
s): 使用
get()
和一个List<String>
作为您的参数(无需专门使它成为ArrayList
):
System.out.println("Value at Index " + i +
" is " + al.get(i));
and 和
public void display(ArrayList<String> al) {
One other thing to "smarten the code" is to not use the specific implementation in the declaration or parameters. “保护代码”的另一件事是不使用声明或参数中的特定实现。
public void exampleArrayList () {
// use the interface List<T>, not the specific implementation ArrayList<T>
List<String> al = new ArrayList<String>();
...
}
// take the Interface, and give it the type
public void display(List<String> al) {
....
}
The functionality will be the same, but it is a better programming approach to program to interfaces rather than implementations. 功能将是相同的,但是它是对接口而不是实现进行编程的更好的编程方法。
EDIT : Also, unless you really need the index for some reason, using the enhance for loop may be more appropriate 编辑 :另外,除非由于某些原因确实需要索引,否则使用Enhance for循环可能更合适
for (String s : al) {
//some operation
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.