[英]I can't get my nested for loop to engage
我正在嘗試編寫一個程序,該程序采用整數數組,掃描並刪除所有重復值,然后將其余值作為ArrayList返回。
我覺得我在正確的方向上是正確的,但是我似乎無法獲得必要的嵌套循環參與。 到目前為止,這是我的代碼。
import java.util.ArrayList;
/**
*
* @author Justin
*/
public class ArrayOps4 {
/**
* @param args
* the command line arguments
*/
public static void main(String [] args) {
int [] cake = {2, 3, 4, 3, 2, 4, 5, 4, 6, 6, 6, 4, 3};
System.out.println(copyArray(cake));
// TODO code application logic here
}
/**
*
* @param anArray
* @return
*/
public static ArrayList copyArray(int [] anArray) {
// your work here
// declare the new ArrayList
ArrayList<Integer> nal = new ArrayList<>();
// loop through anArray, eliminating duplicates,
int [] Array2;
for (int i = 0 ; i < anArray.length ; i++) {
for (int j = 1 ; j < anArray.length ; j++)
if (anArray[j] == anArray[i]) {
for (int m = j ; m < anArray.length ; m++) {
anArray[m] = anArray[m++];
}
}
}
for (int ii = 0 ; ii < anArray.length ; ii++) {
nal.add(anArray[ii]);
}
// storing each unique element in the ArrayList
// your work here
// return new ArrayList
return nal;
// your work here
}
}
其他一切似乎都工作正常,但是第二個for循環(應該循環遍歷數組中的所有整數並將它們與第一個for循環所保持的位置進行比較)不會起作用。 當我在Netbeans中調試它時,第一個for循環的變量會自動遞增,然后移至ArrayList創建循環,效果很好。
對於我在做錯的事情,或者該程序甚至可以完成我認為會起作用的事情,我將不勝感激。
考慮到每個人的有用建議后,我離我很近,但我仍然很困惑。 我修改后的代碼在輸入兩個字符串時仍然遇到問題,我只是想不通為什么
package arrayops4;
import java.util.ArrayList;
/**
*
* @author Justin
*/
public class ArrayOps4 {
/**
* @param args
* the command line arguments
*/
public static void main(String [] args) {
int [] cake = {2, 3, 4, 3, 2, 4, 6, 8, 9, 6, 11};
System.out.println(copyArray(cake));
// TODO code application logic here
}
/**
*
* @param anArray
* @return
*/
public static ArrayList copyArray(int [] anArray) {
// your work here
// declare the new ArrayList
ArrayList<Integer> nal = new ArrayList<>();
// loop through anArray, eliminating duplicates,
int counter = 0;
int length = anArray.length;
for (int i = 0 ; i < anArray.length ; i++) {
for (int b = i + 1 ; b < anArray.length ; b++) {
if (anArray[i] == anArray[b]) {
for (int ii = b ; ii < anArray.length - 1 ; ii++) {
anArray[ii] = anArray[ii + 1];
}
counter++;
}
}
}
length = length - counter;
for (int k = 0 ; k < anArray.length - 1 ; k++) {
nal.add(anArray[k]);
}
return nal;
}
}
**仍然給我帶來麻煩的輸入是[2、14、9、15、9、7、3、17、14]和[3、5、5、5、3、3、5、3、5] 。 第一組返回[2、14、9、15、7、3],但缺少17。第二組返回空括號。 任何進一步的幫助將不勝感激。 提前致謝。 **
您的代碼有兩個問題:
for(int j=1;j<anArray.length;j++)
應該將j
初始化為i+1
以便從下一個元素開始,並且刪除元素的代碼也應該減小數組的有效長度(因為不可能減小實際的長度)。 您需要添加一個int
變量effectiveLength
anArray.length
,將其初始初始化為anArray.length
,並且每次從anArray
刪除元素時將其遞減1。
考慮更改您的算法,以便采用一種侵入性較小,速度更快的算法,該算法可按需構建結果:
anArray
每個元素,檢查其是否存在於nal
nal
所有元素,並將其與anArray[i]
進行比較。 nal
添加anArray[i]
這對於集合論是一個很好的應用。 Iterator類使遍歷集合和刪除元素變得容易。
Integer[] cake= {2,3,4,3,2,4,5,4,6,6,6,4,3};
List<Integer> array = new ArrayList<Integer>();
Collections.addAll(array, cake);
HashSet<Integer> hash = new HashSet<Integer>();
Iterator<Integer> iter = array.iterator();
while(iter.hasNext())
{
Integer val = iter.next();
if (hash.contains(val))
{
iter.remove();
}
else
{
hash.add(val);
}
}
System.out.println(array);
您正在使用后增量:
anArray[m] = anArray[m++];
這不會達到您的預期目的,但是會將值復制到它已經位於的相同索引中。此外,您將m增加兩次,因為for語句已經這樣做了。
這是要點。 你在球場上。 主要的間距是為了引起人們對代碼更改的注意。
for(int i=0;i<anArray.length;i++)
{
for(int j= i+1 ;j<anArray.length;j++)
if(anArray[j] == anArray[i])
{
for(int m=j;m<anArray.length - 1 ;m++)
{
anArray[m] = anArray[m +1 ];
}
}
}
但是,您還必須跟蹤整個數組向[0]移位的次數,以便打印輸出循環僅打印移位后剩余的內容。 否則輸出為2,3,4,5,6 [這應該是結尾],6,3,3,3,3,3,...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.