package exercise;
/*
* 去除數組中爲零的項*/
public class Demo2 {
public static void main(String[] args) {
int oldArray[] = new int[]{1,2,3,4,0,0,0,0,0,5,6,0,7,0,8,0,9};
int newArray[] = new int[count(oldArray)];
copy(oldArray,newArray);
print(newArray);
}
public static int count(int temp[]){
int n = 0;
for(int x = 0; x < temp.length ; x++ ){
if(temp[x]!=0){
n++;
}
}
return n;
}
public static void copy(int old[],int ne[]){
int n = 0;
for(int x = 0;x < old.length; x++){
if(old[x] != 0){
ne[n] = old[x];
n++;
}
}
}
public static void print(int temp[]){
for(int i=0; i< temp.length;i++){
System.out.print(temp[i]+"、");
}
System.out.println();
}
}
24小時熱門文章
-
再談23種設計模式(3):行爲型模式(學習筆記)
-
Power Automate Desktop 安裝完,登錄後老是提示one driver 錯誤
-
微前端學習筆記(4):從微前端到微模塊之EMP與hel-micro方案探索
-
微前端學習筆記(1):微前端總體架構概述,從微服務發微
-
985 碩士程序員,空窗 4 個月沒有 Offer!
-
一文搞懂 Spring 循環依賴
-
賽博鬥地主——使用大語言模型扮演Agent智能體玩牌類遊戲。
-
VScode右鍵打開(添加到右鍵)
-
記一次 .NET某工控視覺自動化系統 卡死分析
-
WindowsServer--SQL Server搭建主從同步實現讀寫分離 - 事務性分發