您好我的程序在int pos = P.get(i);
周围给出了错误它将编译和打印元素和元素的位置,但不会打印
如果你可以帮我弄清楚代码的哪个部分是错的并修复它,那么pos val部分代码
import java.util.*;
public class printlots {
//printlots procedure implementation
public static void printLots(List L, List P){
//display the table heading
System.out.println("pos\tval");
System.out.println("----------");
//repeat the loop for all positions in the list P
for(int i = 0; i < P.size();i++){
//get current position from the list P
int pos = P.get(i);
//get the value (from the list L) stored at the current position
int val = L.get(pos);
//display the position and its corresponding value
System.out.println(pos + "\t" + val);
}
}
//main method
public static void main(String[]args){
//create a list to store several elements
List L = new ArrayList();
//create several elements
List P = new ArrayList();
//add several elements to the list l
L.add(10);
L.add(20);
L.add(30);
L.add(40);
L.add(50);
L.add(60);
L.add(70);
L.add(80);
//add several positions to the list P
P.add(1);
P.add(3);
P.add(4);
P.add(6);
//print the values stored in the two list
System.out.println("Elements: " + L);
System.out.println("Positions: " + P);
System.out.println();
}
}
答案 0 :(得分:0)
请不要使用raw-types。并打电话给你的方法。
List<Integer> L = new ArrayList<>();
List<Integer> P = new ArrayList<>();
// ...
printLots(L, P);
并更改方法声明以采用参数化类型,如
public static void printLots(List<Integer> L, List<Integer> P){
答案 1 :(得分:0)
在main()方法中调用printLots()方法
printLots(L,P);