2016-11-28 26 views
0
List<String> checkLength(List<String> input) { 
    if (input.length > 6) { 
    var tempOutput = input; 
    while (tempOutput.length > 6) { 
     var difference = (tempOutput.length/6).round() + 1; 
     for (int i = 0; i < tempOutput.length - 1; i + difference) { 
     tempOutput.removeAt(i); //Removing the value from the list 
     } 
    } 
    return tempOutput; //Return Updated list 
    } else { 
    return input; 
    } 
} 

我想從臨時列表中刪除某些內容。爲什麼它不起作用?我沒有看到它是如何修復的,在我解決的其他問題中,我使用了類似的方法,它工作(即使幾乎相同)什麼使得這是Dart中的一個固定長度列表?

請注意我對Dart有點新,所以請原諒我這種問題,但我找不出解決方案。

查找達特鏈接

Code in Dart

+0

請將代碼作爲文本直接添加到您的問題中,而不是鏈接的屏幕截圖。 –

+0

你如何創建列表?如果你做'新列表(6)',它將變成一個固定長度的列表,有6個條目。 –

+0

checkLength(arrayToSingularElements(toColorBlockArray(input)))。join(''); –

回答

0

可用的代碼可以確保tempOutput沒有一個固定長度的列表中初始化它作爲

var tempOutput = new List<String>.from(input);

從而宣告tempOutput到是一個可變的副本input

FYI它也看起來你有在你的程序中的另一個bug,因爲你在你的for循環更新步驟做i + difference,但我想你想i += difference

+0

'var tempOutput = input.toList()'是相似的。 –

0

你可以試試這段代碼,讓我知道是那樣嗎?

List<String> checkLength(List<String> input) { 
    if (input.length > 6) { 
    var tempOutput = input; 
    while (tempOutput.length > 6) { 
     var difference = (tempOutput.length/6).round() + 1; 
     for (int i = 0; i < tempOutput.length - 1; i = i + difference) { 
     tempOutput.removeAt(i); //Removing the value from the list 
     } 
    } 
    return tempOutput.toList(); //Return Updated list 
    } else { 
    return input.toList(); 
    } 
} 

注意:使用的「1 +差」,這是例如在第一次迭代中相同的值說你I = 1和差值= 1,則「tempOutput.removeAt(I)」將在刪除值「 1「的位置,再次在第二次迭代中,您嘗試刪除相同的位置,因此錯誤清楚地指出」無法從固定長度移除「

這裏,i值必須爲每個迭代過程遞增或遞減,在缺少的for循環中。

+0

是的,我剛纔已經明白了這一點,並得到了和你一樣的解決方案。 –

+0

感謝@LukeMuller,如果你覺得這個工作正常,那麼你可以接受作爲答案,這可能有助於未來。 –

0

@ harry-terkelsen的答案對解決定長問題非常有幫助。

對於那些詢問我的算法的人: 不同之處在於想要刪除一些字符時跳過字符的數量。此外,我不得不改變for循環,因爲它沒有做到我想要的。

修正在這裏! https://github.com/luki/wordtocolor/blob/master/web/algorithms.dart

謝謝你理解我!

+0

哦,我剛剛意識到@BHUVANESH MOHANKUMAR得到了同樣的問題解決方案。 –

+0

謝謝@Luke Muller –

相關問題