用java实现: 把数组oldArr[] 1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5中不是0的元素存放在新数组newArr[]中
展开全部
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Change {
public static void main(String args[]) {
Integer[] oldArr = { 1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5 };
List<Integer> data = new ArrayList<Integer>();
for (int i=0;i<16;i++) {
if (oldArr[i] != 0) {
data.add(oldArr[i]);
}
}
Integer[] newArr = data.toArray(new Integer[0]);
}
}
代码如上,以后可以继续交流java的相关编程
展开全部
具体步骤如下:
public class Demo {
public static void main(String[] args) {
int j=0;
int oldArr[] = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};
int newArr[] = null;
newArr = new int[oldArr.length];
for(int i = 0 ; i < oldArr.length ; i++) {
if(oldArr[i] != 0) {
newArr[j] = oldArr[i];
System.out.println("newArr["+j+"] = " + newArr[j]);
j++;
}
}
}
}
最简遍的办法,而且容易懂,速度采纳!
public class Demo {
public static void main(String[] args) {
int j=0;
int oldArr[] = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};
int newArr[] = null;
newArr = new int[oldArr.length];
for(int i = 0 ; i < oldArr.length ; i++) {
if(oldArr[i] != 0) {
newArr[j] = oldArr[i];
System.out.println("newArr["+j+"] = " + newArr[j]);
j++;
}
}
}
}
最简遍的办法,而且容易懂,速度采纳!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class $ {
public static void main(String args[]) {
Integer[] oldArr = { 1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5 };
List<Integer> data = new ArrayList<Integer>();
for (Integer em : oldArr) {
if (em != 0) {
data.add(em);
}
}
Integer[] newArr = data.toArray(new Integer[0]);
System.out.println(Arrays.deepToString(newArr));
}
}
结果
[1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5]
import java.util.Arrays;
import java.util.List;
public class $ {
public static void main(String args[]) {
Integer[] oldArr = { 1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5 };
List<Integer> data = new ArrayList<Integer>();
for (Integer em : oldArr) {
if (em != 0) {
data.add(em);
}
}
Integer[] newArr = data.toArray(new Integer[0]);
System.out.println(Arrays.deepToString(newArr));
}
}
结果
[1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5]
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |