我有两个这样的列表:
1. [apple, orange]
2. [cyan, magenta, yellow, black]
我希望算法打印成这样:
1. [apple:cyan,magenta]
2. [orange:yellow, black]
我做错了什么?
public static void main(String[] args) throws IOException {
String file_name_Colors = "D:/colors.txt";
String file_name_Fruts = "D:/fruts.txt";
Scanner input = new Scanner(System.in);
int number_Colors;
int number_Fruts;
number_Colors = input.nextInt();
number_Fruts = input.nextInt();
try{
ReadFileColors fileColors = new ReadFileColors(file_name_Colors, number_Colors);
ReadFileFruts fileFruts = new ReadFileFruts(file_name_Fruts, number_Fruts);
String[] aryLines = fileColors.OpenFileColors();
String[] aryLinesFruts = fileFruts.OpenFileFruts();
List<String> listColorPleaseTY = new ArrayList<>();
List<String> listFruts_Fruts = new ArrayList<>();
int numberColorPlease;
int numberFrutsPlease;
for(numberColorPlease=0; numberColorPlease<aryLines.length; numberColorPlease++){
listColorPleaseTY.add(aryLines[numberColorPlease]);
}
for(numberFrutsPlease=0; numberFrutsPlease<aryLinesFruts.length; numberFrutsPlease++){
listFruts_Fruts.add(aryLinesFruts[numberFrutsPlease]);
}
System.out.println(listFruts_Fruts);
System.out.println(listColorPleaseTY);
List<String> subItemslistnewone = new ArrayList<String>(listColorPleaseTY.subList(0, number_Colors));
System.out.println(subItemslistnewone);
} catch (IOException e){ System.out.println( e.getMessage() ); }
}
答案 0 :(得分:0)
我不确定分配标准,你需要这样的东西吗?
public static void main(String[] args) {
List<String> fruits = Arrays.asList("apple","orange");
List<String> colours = Arrays.asList("cyan","magenta","yellow","black");
List<String> result = new ArrayList<>(fruits.size());
for (int i = 0; i < fruits.size(); i++) {
StringBuilder sb = new StringBuilder();
sb.append(fruits.get(i));
sb.append(":");
int begin = (i*colours.size())/fruits.size() + 1;
int end = ((i+1)*colours.size())/fruits.size();
sb.append(colours.get(begin-1));
for (int j = begin; j < end; j++) {
sb.append(",");
sb.append(colours.get(j));
}
result.add(sb.toString());
}
for (int i = 0; i < result.size(); i++) {
System.out.println((i+1)+". ["+result.get(i)+"]");
}
}