import ...
@Component({ ... })
export class TestComponent implements OnInit {
// A Couple of @Input statements
@Input() ...
// Create Formcontrollers
controlNumber = new FormControl("", [Validators.pattern("[0-9]*")]);
controlText = new FormControl("", [Validators.pattern("[A-Z-a-z]*")]);
datePicker = new FormControl(moment());
constructor(private http: HttpClient) { }
ngOnInit() {
}
...
getNotANumberMessage() {
return this.controlNumber .hasError("pattern") ? "Numbers only" : "";
}
getNotAStringMessage() {
return this.controlText.hasError("pattern") ? "Strings only" : "";
}
}
这是我目前得到的:
int curr = 0;
int cnt;
String element = values[0][0];
int numberRepeats = 0;//cnt-counter,what's the element ,how many times it's repeated
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < values[i].length; j++) {//those two for's are for the current element
cnt = 0;//counter is nullified
for (int j2 = i; j2 < values.length; j2++) {
for (int k = 0; k < values[j2].length; k++) {//and those two are the compared element
if (values[i][j] == values[j2][k]) {//if the current element is the same as the compared element,increase counter
cnt++;
}
}
if (cnt >numberRepeats) {//after the compared element is done comparing and the number of repeats of the current element is more then the lastly checked element
element = values[i][j];//we get the element ,and how many times it's repeated
numberRepeats = cnt;
}
}
}
}
System.out.println();
System.out.println("The most popular item is: "+element+". Number sold:"+numberRepeats);`
这就是我想要得到的:
houseShampoo meatPork dairyCream wheatBread wheatCrackers
houseShampoo houseShampoo houseDetergent meatPork dairyYogurt
meatLamb dairyMilk dairyCream meatPork houseShampoo
wheatCookies meatLamb dairyYogurt wheatCereal wheatBread
meatLamb dairyMilk wheatCookies wheatCrackers wheatPasta
The most popular item is: houseShampoo. Number sold:4
但是我不知道如何更改The least popular item is: wheatPasta. Number sold:1
语句中的条件以给出最不频繁的元素,而不是最流行的元素
答案 0 :(得分:0)
由于您希望找到最少的重复次数,因此请从最大值的初始值开始:
int numberRepeats = Integer.MAX_VALUE;
,当发现重复次数较少的项目时,将其替换:
if (cnt < numberRepeats) {
element = values[i][j];
numberRepeats = cnt;
}
这应该可以解决问题。
答案 1 :(得分:-1)
嗨,我选择了使用Map的其他方法,希望对您有所帮助:)
int dimension = 3;
int sold = 0;
String product = "";
Map<String,Integer> productsSoldAndCount = new HashMap<>();
String[][] elements = new String[3][3];
elements[0] = new String[]{"houseShampoo","meatpork","dairyCream"};
elements[1] = new String[]{"houseShampoo","bread","cookies"};
elements[2] = new String[]{"meatpork","meatpork","meatpork"};
for(var tmpArray : elements){
for(var tmpString : tmpArray){
if(!productsSoldAndCount.containsKey(tmpString)){
productsSoldAndCount.put(tmpString,1);
}else{
productsSoldAndCount.put(tmpString,productsSoldAndCount.get(tmpString) + 1);
}
}
}
for(var resultPair : productsSoldAndCount.entrySet()){
if(resultPair.getValue() > sold){
sold = resultPair.getValue();
product = resultPair.getKey();
}
}
System.out.println("The most popular item is: " + product + " Sold: " + sold);