你好,我的程序在 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();
}
}
最佳答案
请不要使用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){
关于java - 程序错误不会打印部分代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33049031/