[英]How can I create a program that stores a numerous amount of names/numbers in an array by clicking a button without the method resetting my counter?
[英]How can I create a java program to find the amount of consecutive numbers in an array?
我正在嘗試制作一個 Java 程序來查找數組中連續數字的數量。 例如,如果一個數組具有值 1,8,10,4,2,3,則有 4 個連續的數字 (1,2,3,4)。 我已經創建了這個程序,但是在第 28 行和第 31 行出現 ArrayIndexOutOfBoundsException 錯誤,我該如何解決這個錯誤? (如果錯誤得到修復,我什至不確定我制作的程序是否可以工作)。 注意:我知道網上有很多解決方案,但我是一名初學者程序員,我正在嘗試以更簡單的方式做到這一點。
import java.util.Arrays;
class Main {
public static void main(String[] args) {
consec();
}
static void consec()
{
int[] nums = {16, 4, 5, 200, 6, 7, 70, 8};
int counter=0;
Arrays.sort(nums);
for (int i=0; i < nums.length; i++)
if (i != nums.length - 1)
System.out.print(nums[i] + ", ");
else
System.out.print(nums[i]);
for (int i=0; i < nums.length; i++)
for (int j=i; j < nums.length - i; j++)
if (nums[j + 1] - 1 == nums[j])
counter++;
else if (nums[j+1]==counter)
System.out.print("Consective amount is" + counter);
}
}
異常的問題在於nums[j + 1]
的訪問。 請注意,由於 for 循環, j 可以與nums.length - 1
一樣大。 因此 j + 1 可以是 nums.length ,它是 OutOfBounds 數組索引。
其次,我認為您的代碼不能解決任務 - 例如,如果您計算的連續數字的數量出現在數組中,您只會打印結果。 但是我不明白這些事情應該如何相關。 您可以這樣解決問題:
for (int i = 1; i < nums.length; i++) {
if (nums[i-1] == nums[i] - 1)
counter+= 2;
int j = i + 1;
while (j < nums.length && nums[j] - 1 == nums[j-1]) {
j++;
counter++;
i = j;
}
System.out.print("Consective amount is" + counter);
請注意,索引 i 從 1 開始,因此我們可以確定 nums[i-1] 存在。 如果 nums 只有一個元素,我們不應該遇到任何問題,因為條件i < nums.length
不會被滿足。 我們為序列的每個開始計算兩個結果,為每個后續結果(while 循環)計算一個添加元素。 當序列結束時,我們嘗試通過將索引 i 移動到最后一個序列的末尾 (j = i) 來找到它后面的新序列。
上面的代碼將對多個不同的連續數字序列求和。 例如數組 [17,2,20,18,4,3] 有五個結果數(2,3,4 和 17,18)
該算法在 O(n) 內具有時間復雜性,因為我們將 i 或 j 至少增加 on 並在每個序列之后跳過 i 到 j。
我建議重新考慮您掃描陣列的方法。 理想情況下,您應該只需要一個 for 循環來解決這個問題。
我個人創建了一個 HashSet of Numbers,它不能包含重復項。 從那里,您可以從1
迭代到nums.length-1
,並檢查nums[i] - 1 == nums[i-1]
(即:它們是否是連續的)。 如果它們相等,您可以將兩個數字添加到 HashSet。
最后,您實際上有一組連續數字,但是對於這個問題,您可以簡單地返回該組的大小。
我強烈建議你嘗試這個問題並按照我的解釋。 如果您只需要代碼,這就是我想出的方法。
public static int countConsecutive(int[] nums) {
Set<Integer> consecutive = new HashSet<>();
if (nums.length <= 1)
return 0;
Arrays.sort(nums);
for (int i = 1; i < nums.length - 1; i++) {
if (nums[i] != nums[i - 1] + 1)
continue;
consecutive.add(nums[i]);
consecutive.add(nums[i - 1]);
}
return consecutive.size();
}
這是另一種不需要排序的方法。 它使用一個位集。 和你的例子一樣,是假設正數。
int[] values = {4, 3, 10, 11, 6, 1, 4, 8, 7};
根據值設置相應的位位置。
BitSet bits = new BitSet();
for (int i : values) {
bits.set(i);
}
初始化 output 的一些值、起始位 position 和設置長度。
BitSet out = new BitSet();
int start = 0;
int len = bits.length();
現在遍歷位集,找到占據相鄰位置的位范圍。 這些將表示填充原始 BitSet 時生成的連續序列。 僅顯示兩個或更多的序列。
while (start < len) {
start = bits.nextSetBit(start);
int end = bits.nextClearBit(start+1);
if (start != end-1) {
// populate the subset for output.
out.set(start,end);
System.out.println(out);
}
out.clear();
start = end;
}
印刷
{3, 4}
{6, 7, 8}
{10, 11}
如果您只想要最大的計數,而與實際值無關,那就更簡單了。 只需在初始化位集后使用它來代替上述內容。
int start = 0;
int len = bits.length();
int total = 1;
while (start < len) {
start = bits.nextSetBit(start);
int end = bits.nextClearBit(start + 1);
total = Math.max(total, end-start);
start = end;
}
System.out.println(total);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.