尝试为 subsetSum 编写算法...它应该找到给定向量的所有可能子集,然后找到哪些子集加起来达到目标值。但是,我不断收到 nullpointerexceptions 和其他一些错误。有人可以帮我吗?我处于一个紧张的位置,大脑几乎无法运作。非常感激。谢谢。
java.lang.NullPointerException
at Sumation.subsetSum(Sumation.java:78)
at Sumation.main(Sumation.java:110)
第 78 行是 subsetSum 方法中第一个 for 循环的行。
/* Ruben Martinez
* CS 210 Data Structures
* Program requires no paramters at main method call;
* instead, a JOptionPane asks for the ints the user
* wishes to search. These must be seperated by commas,
* e.g. 50,40,30 or 35,45,55. Program then asks for a
* target int to find. Program searches for target
* and returns combinations that add up to the target.
*/
import java.util.Vector;
import javax.swing.*;
public class Sumation
{
static int[] array;
static int target;
static Vector<Integer> subsets;
static Vector<Integer> set;
static Vector<Vector<Integer>> outer;
public Sumation() {
//insert integers into array
String defineArray = (String)JOptionPane.showInputDialog(null,
"Enter integers to search. Seperate by commas.", null);
//splits string into array delimeted by commas
String[] arrayString = defineArray.split(",");
//creates int array of size of string array
array = new int[arrayString.length];
//adds ints from args[] to int array
for (int i = 0; i < arrayString.length; i++) {
array[i] = Integer.parseInt(arrayString[i]);
}
//enter integer to search for
String targetString = (String)JOptionPane.showInputDialog(null,
"What is your target integer?", null);
//turns string to int
target = Integer.parseInt(targetString);
set = new Vector<Integer>();
for (int n = 0; n < array.length; n++) {
set.add(array[n]);
}
}
private static Vector<Vector<Integer>> getSubsets(Vector<Integer> set) {
Vector<Vector<Integer>> subsetCollection = new Vector<Vector<Integer>>();
if (set.size() == 0) {
subsetCollection.add(new Vector<Integer>());
} else {
Vector<Integer> reducedSet = new Vector<Integer>();
reducedSet.addAll(set);
int first = reducedSet.remove(0);
Vector<Vector<Integer>> subsets = getSubsets(reducedSet);
subsetCollection.addAll(subsets);
subsets = getSubsets(reducedSet);
for (Vector<Integer> subset : subsets) {
subset.add(0, first);
}
subsetCollection.addAll(subsets);
}
return subsetCollection;
}
public static Vector<Vector<Integer>> subsetSum(Vector<Integer> subsets, int target) {
//creates outer vector
outer = new Vector<Vector<Integer>>();
for (int k = 0; k < subsets.size(); k++) {
//if k is the target, display
if (array[k] == target) {
//creates new inner vector for values that equal target
Vector<Integer> inner = new Vector<Integer>();
outer.add(inner);
//add k to vector
inner.add(array[k]);
}
for (int l =0; l < subsets.size(); l++) {
int sum = subsets.elementAt(k);
if (sum == target) {
//creates new inner vector for values that sum up to target
Vector<Integer> inner = new Vector<Integer>();
outer.add(inner);
//add l,k to vector
inner.add(array[l]);
inner.add(array[k]);
}
else {
System.out.print("");
}
}
}
//return combinations that add up to target in vector form
return outer;
}
public static void main(String[] args) {
//calls sumation constructor
Sumation s = new Sumation();
s.getSubsets(set);
s.subsetSum(subsets, target);
JOptionPane.showMessageDialog(null, "The combinations that equal to "+target+" are \n"+outer, "Vector", JOptionPane.INFORMATION_MESSAGE);
}
}