将新的 HashSet 添加到先前的 HashSet,"CHANGING" 先前的 HashSet 并继续这样做直到满足特定条件
Add a new HashSet to a previous HashSet, "CHANGING" the previous HashSet and continue doing so until certain condition has been met
我知道如果你有两个 HashSet
你可以创建第三个添加 two.However,为了我的目的我需要改变我以前的 HashSet
,寻找某些condition ,然后如果不满足则更改设置 again.My 目的是我将给出一个输入,比如数字 456
,并查找数字(1 到 9,包括 0)。如果我'我找不到 HashSet
的尺寸 10 然后我将数字乘以 2 ,然后执行 same.So 我会得到 912
;现在大小是 6(我需要得到所有数字 1-9 和 0,即大小 10)。现在我将它乘以 3 得到 2736
,大小现在是 7.I继续这样做,直到我得到大小 10.At 当我得到大小 10 时,我将完成循环并且 return 结束循环的最后一个数字,按照增量乘法 rule.My 方法是follows.It 有错误所以不会 运行 但它代表了我目前的理解。
public long digitProcessSystem(long N) {
// changing the passed in number into String
String number = Long.toString(N);
//splitting the String so that I can investigate each digit
String[] arr = number.split("");
// Storing the digits(which are Strings now) into HashSet
Set<String> input = new HashSet<>(Arrays.asList(arr));
// Count starts for incremental purpose later.
count =1;
//When I get all digits; 1-9, & 0, I need to return the last number that concluded the condition
while (input.size() == 10) {
return N;
}
// The compiler telling me to delete the else but as a new Java user so far my understanding is that I can use `else` with `while`loops.Correct me if I'm missing something.
else {
// Increment starts following the rule; N*1, N*2,N*3,...till size is 10
N = N*count;
// doing everything over
String numberN = Long.toString(N);
String[] arr1 = number.split("");
// need to change the previous `input`so that the new updated `HashSet` gets passed in the while loop to look for size 10.This is error because I'm using same name `input`. But I don't want to create a new `set` , I need to update the previous `set` which I don't know how.
Set<String> input = new HashSet<>(Arrays.asList(arr1));
// increments count
count++;
}
clear()
input
并添加新值。像
// Set<String> input = new HashSet<>(Arrays.asList(arr1));
input.clear();
input.addAll(Arrays.asList(arr1));
和
while (input.size() == 10) {
应该是
if (input.size() == 10) {
或者您的 else
未绑定到 if
。
我知道如果你有两个 HashSet
你可以创建第三个添加 two.However,为了我的目的我需要改变我以前的 HashSet
,寻找某些condition ,然后如果不满足则更改设置 again.My 目的是我将给出一个输入,比如数字 456
,并查找数字(1 到 9,包括 0)。如果我'我找不到 HashSet
的尺寸 10 然后我将数字乘以 2 ,然后执行 same.So 我会得到 912
;现在大小是 6(我需要得到所有数字 1-9 和 0,即大小 10)。现在我将它乘以 3 得到 2736
,大小现在是 7.I继续这样做,直到我得到大小 10.At 当我得到大小 10 时,我将完成循环并且 return 结束循环的最后一个数字,按照增量乘法 rule.My 方法是follows.It 有错误所以不会 运行 但它代表了我目前的理解。
public long digitProcessSystem(long N) {
// changing the passed in number into String
String number = Long.toString(N);
//splitting the String so that I can investigate each digit
String[] arr = number.split("");
// Storing the digits(which are Strings now) into HashSet
Set<String> input = new HashSet<>(Arrays.asList(arr));
// Count starts for incremental purpose later.
count =1;
//When I get all digits; 1-9, & 0, I need to return the last number that concluded the condition
while (input.size() == 10) {
return N;
}
// The compiler telling me to delete the else but as a new Java user so far my understanding is that I can use `else` with `while`loops.Correct me if I'm missing something.
else {
// Increment starts following the rule; N*1, N*2,N*3,...till size is 10
N = N*count;
// doing everything over
String numberN = Long.toString(N);
String[] arr1 = number.split("");
// need to change the previous `input`so that the new updated `HashSet` gets passed in the while loop to look for size 10.This is error because I'm using same name `input`. But I don't want to create a new `set` , I need to update the previous `set` which I don't know how.
Set<String> input = new HashSet<>(Arrays.asList(arr1));
// increments count
count++;
}
clear()
input
并添加新值。像
// Set<String> input = new HashSet<>(Arrays.asList(arr1));
input.clear();
input.addAll(Arrays.asList(arr1));
和
while (input.size() == 10) {
应该是
if (input.size() == 10) {
或者您的 else
未绑定到 if
。