Java 在方法之间传递ArrayList
在关于数组和ArrayList的继续教育中,我试图通过将ArrayList从一种方法传递到另一种方法来改进代码。这是我的密码:Java 在方法之间传递ArrayList,java,arraylist,Java,Arraylist,在关于数组和ArrayList的继续教育中,我试图通过将ArrayList从一种方法传递到另一种方法来改进代码。这是我的密码: public void exampleArrayList () { ArrayList<String> al = new ArrayList<String>(); al.add("AZ"); al.add("BY"); al.add("CX"); al.add("DW"); al.add("EV")
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)));
}
我尝试将I
更改为硬编码数字,但仍然失败,我不知道原因。1)您需要将其作为数组列表传递:
公共空白显示(ArrayList al){
^^^^^^^^
2) 您正在搜索列表中的整数。列表不包含任何整数,因此indexOf
返回-1。然后调用al.get(-1)
其中-1显然超出了范围。我不确定您在这里要做什么。1)您需要将其作为数组列表传递:
公共空白显示(ArrayList al){
^^^^^^^^
2) 您正在搜索列表中的整数。列表不包含任何整数,因此indexOf
返回-1。然后调用al.get(-1)
where-1显然超出了范围。我不确定您在这里想做什么。您正在使用的,给定int
,它将搜索该int
,如果列表包含它,则返回其索引。因为不是这种情况-它是一个列表
-您得到的索引超出了范围,因为您试图检索一个元素如果找不到元素(它找不到),则从indexOf()
返回索引-1.-1处的nt
这就是为什么不应该使用原始类型。使用和列表作为参数(无需特别指定ArrayList
s):
及
公共空白显示(ArrayList al){
您正在使用的,如果列表中包含该int
,它将搜索该int
,并返回其索引。事实并非如此-它是一个列表
-您得到的索引超出了范围,因为您试图在索引-1处检索元素。-1从indexOf()返回
如果找不到元素,则无法找到该元素
这就是为什么不应该使用原始类型。使用和列表作为参数(无需特别指定ArrayList
s):
及
公共空白显示(ArrayList al){
另一个“智能化代码”的方法是不要在声明或参数中使用特定的实现
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) {
....
}
“智能化代码”的另一件事是不要在声明或参数中使用特定的实现
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) {
....
}
我想你的意思是“索引的值I
是al.get(I)
”?完全正确的板球。我想你的意思是“索引的值I
是al.get(I)
”完全正确的蟋蟀。我只是想让自己陷入困境。这正是我需要知道的。将它作为字符串传递。然后我看到我在索引方面的明显错误,因为我在头脑中混淆了两段不同的代码。我想是时候休息一下了。谢谢你的帮助。慢慢地,这些东西开始变得有用了nse.Airfix。这正是我需要知道的。将其作为字符串传递。然后我看到了关于索引的明显错误,因为我在头脑中混淆了两段不同的代码。我想是时候休息一下了。谢谢你的帮助。慢慢地,这些东西开始有意义了。Airfix。如果我理解第一个注释ArrayList是一个列表的实现你基本上是说我通过不调用实现(在我的java基础知识水平上我还不理解)来获得更大的灵活性.关于增强循环,我来自fortran、C和visual basic的基础知识,因此这是我在面向对象编程方面的专长,我仍在学习。如果有疑问,我会回到我的基础上来。@Airfix有许多好处,正如答案所示。.通常,您希望保留更改实现的能力如果需要的话。今天你有一个ArrayList
,但也许明天一个LinkedList
更合适。如果你在参数中有List
,你可以调整实现,而另一个代码是不知道的。如果我理解第一个注释ArrayList是一个列表的实现,你基本上是说我有通过不调用实现(我对java的基本知识还不了解),以某种方式提高灵活性.关于增强循环,我来自fortran、C和visual basic的基础知识,因此这是我在面向对象编程方面的专长,我仍在学习。如果有疑问,我会回到我的基础上来。@Airfix有许多好处,正如答案所示。.通常,您希望保留更改实现的能力如果需要。今天您有一个ArrayList
,但明天可能会有一个LinkedList
更合适。如果参数中有List
,您可以调整实现,而其他代码则不知道。
System.out.println("Value at Index " + i +
" is " + al.get(i));
public void display(ArrayList<String> al) {
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) {
....
}
for (String s : al) {
//some operation
}