array1[count] !=1这个条件,把数组中值为1的排除了,所以出现你上面说的情况。
解决方案 »
- oracle 的时间怎么转为yyyyMMddHHmmss
- 在HQL中用聚集函数查询的问题
- 兄弟们我下载了一个j2eesdk-1_4_02_2005Q2-windows-ml.exe安装完成后,安装目录里面还有jdk目录.....
- 大吓:怎么打开[Borland.Jbuilder.2005.企业版]?在线等?急
- wincap和Jpcap怎么安装在JAVA里,求指导,给分题??
- java数组如何实现变量静态效果
- 怎么把一个很大的double类型的数转换能String类型,不是科学计数法的那种
- 哪位介绍一些信息发布方面的开源项目
- 关于Applet的异常:com.ms.security.SecurityExceptionEx。
- 求一个java用JFrame+JPanel做的小案例,在线等
- 在applet中如何加入菜单???
- Thread问题!
class CopyArrayWhile {
public static void main(String[] arguments) {
int[] array1 = { 7, 4, 8, 1, 4, 1, 4 };
float[] array2 = new float[array1.length]; System.out.print("array1: [ ");
for (int i=0; i<array1.length; i++) {
System.out.print(array1[i] + "; ");
}
System.out.println("]"); System.out.print("array2: [ ");
int count=0;
while ( count<array1.length ) {
array2[count]=(float) array1[count];
System.out.print(array2[count++] + ";");
} System.out.println("]");
}
}
---------- java ----------
arraylength=7
array1: [ 7; 4; 8; 1; 4; 1; 4; ]
array2: [ 7.0 ;4.0 ;8.0 ;1.0 ;4.0 ;1.0 ;4.0 ;]
Normal Termination
Output completed (0 sec consumed).
看看,这是你要的结果吗?
你的循环条件这么写的话,当遇到array1中的第一个1时,条件就不满足了,所以循环就终止了,后面的两个4也就没有输出。
你要想达到过滤掉array1中的两个1的目的,可以这么写:
public class CopyArrayWhile {
public static void main(String[] arguments) {
int[] array1 = { 7, 4, 8, 1, 4, 1, 4 };
float[] array2 = new float[array1.length]; System.out.print("array1: [ ");
for (int i=0; i<array1.length; i++) {
System.out.print(array1[i] + " ");
}
System.out.println("]"); System.out.print("array2: [ ");
int count1 = 0;
int count2 = 0;
while ( count1 < array1.length ) {
if(array1[count1] != 1){
array2[count2]=(float) array1[count1];
System.out.print(array2[count2++] + " ");
}
count1++;
} System.out.println("]");
}
}
你应该这样写
while ( count<array1.length ) {
if (array1[count] !=1)
{
array2[count]=(float) array1[count];
System.out.print(array2[count++] + " ");
}
}