public class Library {
public static int[] histogram(int a[],int M) {
int[] newArr = new int[M];
//Fill the new array
try {
if(a.length<M)
System.out.println("Array lenght should be "
"bigger than the number");
else
for(int i = 0; i < a.length; i ){
int count = a[i];
newArr[count] ;
}
}
catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
//return the array
return newArr;
}
public void printArray(int s[]) {
int position = 0;
for (int i = 0; i < s.length; i ) {
System.out.print(position " ");
position ;
}
System.out.println();
for (int i = 0; i < s.length; i ) {
System.out.print(s[i] " ");
}
}
public static void main(String[] args) {
Library l1 = new Library();
int J = 5;
int[] w = {1,2,0,1,2,3};
l1.printArray(histogram(w,J));
}
}
我寫了這個以及我從谷歌和其他來源查看的一些部分,但我無法理解 public static int[] histogram 中的 else 部分
else
for(int i = 0; i < a.length; i ){
int count = a[i];
newArr[count] ;
}
這個 newArr[count] 是怎么做的;作品有人可以向我解釋,請
uj5u.com熱心網友回復:
` 這個 newArr[count] 是怎么做的;作品有人可以向我解釋,請
這條線上正在發生兩件事情。
- 我們
count從newArr陣列中獲取對位置值的參考。 - 我們將其增加 1。
因此,如果您有陣列newArr = [1,2,3]呼叫newArr[0] 將導致您具有以下狀態newArras [2,2,3]。
如果仍有不清楚的地方,請對此答案發表評論。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/351824.html
