import java.util.*;
class Main {
public static class Pair{
public int left;
public int right;
public void pair(int left, int right){
this.right = right;
this.left = left;
}
}
public void maxprofit(Pair[] arr){
int pro=0;
int cost=0;
int j = arr.length;
HashSet<Integer> hs = new HashSet<Integer>();
for(int i = 0;i<arr.length-1;i++){
while(j>0){
if(arr[i].left+arr[j].left>2){
pro = arr[i].right+arr[j].right;
System.out.println(pro);
}
}
}
}
public static void main(String[] args) {
Main m = new Main();
Pair[] arr = new Pair[4];
arr[0].left=4; arr[0].right=20;
arr[1].left=1; arr[1].right=10;
arr[2].left=1; arr[2].right=40;
arr[3].left=1; arr[3].right=30;
m. maxprofit(arr);
}
}
如您所见,我想打印出表示最大利润的专业版,然后将一些对添加到数组中,但是当我运行这些代码时,java返回了nullpointerexception。
顺便说一句,我是否以正确的方式将配对添加到arr []中?预先感谢大家;
错误消息:
OpenJDK Runtime Environment (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4)
javac -classpath .:/run_dir/junit-4.12.jar -d . Main.java
java -classpath .:/run_dir/junit-4.12.jar Main
Exception in thread "main" java.lang.NullPointerException
at Main.main(Main.java:36)
exit status 1