2016-04-09 13 views
1

İki tane HashSet varsa, ikiyi ekleyerek üçüncü bir tane yaratabileceğinizi biliyorum. benim amacım, önceki HashSet'umu değiştirmem gerekiyor, belirli bir koşulu aramalıyım ve eğer karşılanmazsa, seti tekrar değiştiriniz. Benim amacım, 456 no'lu bir giriş vereceğim ve rakamları aramalıyım (1'den 9'a kadar, 0 dahil 0) Eğer HashSet için 10 boyutunu bulamıyorsam, sayıyı 2 ile çarpacağım ve aynısını yapacağım. Ben 912 alacağım; boyutu 6 şimdi (ve 1-9 & 0, yani, boyut 10) tüm rakamlar almak gerekir. Şimdi 3 ile çarpın ve ben 2736 olsun, boyutu şimdi 7 olur. boyutu 10.Açık 10 olsun zaman, ben döngü tamamlar ve artan çarpım kuralı aşağıdaki döngü, son sonucunu döndürür.Sonraki yaklaşım follows.It hatalar bu yüzden çalışmayacak ama benim temsil eder şimdi anlıyorum.Önceki bir HashSet'e yeni bir HashSet ekleyerek, önceki HashSet'i "DEĞİŞTİRME" ve belirli bir koşul karşılanıncaya kadar devam edin

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++; 

    } 

cevap

0

clear()input ve yeni değerler eklemek.

// Set<String> input = new HashSet<>(Arrays.asList(arr1)); 
input.clear(); 
input.addAll(Arrays.asList(arr1)); 

ve

while (input.size() == 10) { 

gibi bir şey olmalı

if (input.size() == 10) { 

Ya senin else bir if bağlı değildir.

+0

Başka bir sözdizimi hatası alıyorum. Bu [email protected] –

+0

silmek için söylüyordur Muhtemelen, bir "if" olmalıdır. –

+0

Ama ben durumu elde edene kadar geçiş yapmak istiyorum. @ Elliott –

İlgili konular