我想阅读一个包含以下信息的文件:
4 //no. of machines
1,3,6,1 //timings for machine 1
1,3,6,1 //timings for machine 2
1,3,6,1 //timings for machine 3
1,3,6,1 //timings for machine 4
2,4,8,10 //cost for machine 1
2,4,8,10 //cost for machine 2
2,4,8,10 //cost for machine 3
2,4,8,10 //cost for machine 4
没有。时序阵列和成本数组取决于否。机器索引中的元素&#39; i&#39;时序数组与成本数组中的相同索引相关联。例如,机器1的时间1导致成本为2,时间3成本为4.任何人都可以建议哪种数据结构对于这种情况是完美的,我该如何处理它?</ p>
提前致谢。
答案 0 :(得分:3)
您可以使用地图定义timing
和cost
之间的关系。或者,您可以使用TimeCost类来了解时间成本关系。
public class TimeCost{
private Integer time;
private Integer cost;
}
public class Machine{
// private Map<Integer,Integer> timeCost; //possible alternative
private List<TimeCost> timeCost;
}
答案 1 :(得分:2)
首先,您应该使用Pair
来保存时间和费用之间的关系:
class Pair {
int timing;
int cost;
}
然后,我假设您没有收到有关您获得多少时间/费用的信息,因此您必须使用ArrayList
来表示机器的信息:
class Machine {
ArrayList<Pair> timingAndCost;
}
然后,由于为您指定了计算机数量,请使用固定大小的数组来存储计算机:
Machine[] machines = new Machine[numberOfMachines];
如果您正考虑将ArrayList
存储在数组中,请执行以下操作:
ArrayList<Pair>[] machines = new ArrayList<Pair>[numberOfMachines];
它不起作用,因为Java不允许您声明泛型数组。但如果您真的不喜欢Machine
课程,那么您只需使用ArrayList
来存储ArrayList<Pair>
:
ArrayList<ArrayList<Pair>> machines = new ArrayList<>(numberOfMachines);
答案 2 :(得分:1)
import java.util.ArrayList;
class DaMachine {
private final int mMachineIndex;
private final int[] mTimings;
private final int[] mCosts;
public DaMachine(final int pMachineIndex, final int[] pTimings, final int[] pCosts) {
mMachineIndex = pMachineIndex;
mTimings = pTimings;
mCosts = pCosts;
}
@Override public String toString() {
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < mTimings.length; i++) {
sb.append(mTimings[i] + ":" + mCosts[i] + " ");
}
return "Machine [#" + mMachineIndex + ": " + sb + "]";
}
}
class CrazyFile {
static public String CRAZY = "4 //no. of machines \r\n" + //
"1,3,6,1 //timings for machine 1\r\n" + //
"1,3,6,1 //timings for machine 2\r\n" + //
"1,3,6,1 //timings for machine 3\r\n" + //
"1,3,6,11 //timings for machine 4\r\n" + //
"2,4,8,10 //cost for machine 1\r\n" + //
"2,4,8,10 //cost for machine 2\r\n" + //
"2,4,8,10 //cost for machine 3\r\n" + //
"2,4,8,111 //cost for machine 4";
}
public class ReadCrazyFile {
public static void main(final String[] args) {
final String[] lines = CrazyFile.CRAZY.replace("\r\n", "\n").replace("\r", "\n").split("\n");
final int numberOfMachines = Integer.parseInt(cleanString(lines[0]));
final ArrayList<String> timingStrings = new ArrayList<>(numberOfMachines);
final ArrayList<String> costStrings = new ArrayList<>(numberOfMachines);
int offset = 1;
// add timings
for (int relIndex = 0; relIndex < numberOfMachines; relIndex++) {
timingStrings.add(cleanString(lines[offset + relIndex]));
}
offset += numberOfMachines;
// add costs
for (int relIndex = 0; relIndex < numberOfMachines; relIndex++) {
costStrings.add(cleanString(lines[offset + relIndex]));
}
offset += numberOfMachines; // not necessary unless used later
// convert into objects
final ArrayList<DaMachine> machines = new ArrayList<>(numberOfMachines);
for (int machineIndex = 0; machineIndex < timingStrings.size(); machineIndex++) {
final String timingString = timingStrings.get(machineIndex);
final String costsString = costStrings.get(machineIndex);
final int[] timings = convertToIntArr(timingString);
final int[] costs = convertToIntArr(costsString);
final DaMachine terminator = new DaMachine(machineIndex, timings, costs);
machines.add(terminator);
}
for (final DaMachine m : machines) {
System.out.println("" + m);
}
}
static private String cleanString(final String pString) {
return pString.split("//")[0].trim();
}
static private int[] convertToIntArr(final String pTimingString) {
final String[] words = pTimingString.split(",");
final int[] ret = new int[words.length];
for (int i = 0; i < words.length; i++) {
ret[i] = Integer.parseInt(words[i].trim());
}
return ret;
}
}
答案 3 :(得分:1)
这是解决问题所需的结构。
ArrayList<ArrayList<Integer>> listOLists = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> list1= new ArrayList<Integer>();
list1.add(4);
listOLists.add(list1)
ArrayList<Integer> list2= new ArrayList<Integer>();
list2.add(1);
list2.add(3);
list2.add(6);
list2.add(1);
listOLists.add(list2);
ArrayList<Integer> list3= new ArrayList<Integer>();
list3.add(1);
list3.add(3);
list3.add(6);
list3.add(1);
listOLists.add(list3);
像这样明智的你可以继续。它只是一个粗略的想法,请根据您的需要优化此代码。