[英]Java removing zeros from array
我正在尝试编写一个程序,该程序在执行时将遍历一个数组并删除所有0.0的实例,并将数组的大小更改为等于非零元素的数量,并将这些元素按其先前顺序放置。 也就是说,如果n = 10并且a [j]的内容,则j = 0到n-1最初是
0.0, 1.2, 0.0, 0.0, 0.0, 2.3, 0.0, 9.7, 5.6, 0.0
然后在执行代码后,内容应为
n=4, a[0]=1.2, a[1]=2.3, a[2]=9.7, and a[3]=5.6.
这是我到目前为止的内容:
import java.util.Scanner;
public class hw2
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
final double KEY = 0.0;
int n = scan.nextInt();
double[] a = new double[n];
for(int i=0; i<n; i++)
{
a[i] = scan.nextDouble();
}
for(int k = 0; k<n; k++)
{
if(a[k] == KEY)
{
a[k] = a[k+1];
n--;
}
System.out.println(a[k]);
}
}
}
只要朝正确的方向稍加推动,我们将不胜感激。
考虑使用ArrayList ,它将允许您在需要时添加项目,根据需要增长并保持准确的计数。
尽管在这种情况下,如果您希望/需要使用数组,则可能根本无法保存该值为“ 0”的值。 (也不要增加“使用的数组变量”,除非添加时,否则知道多少包含“非0”数据或将迭代过程中遇到的第一个“ 0”视为“有用数据的结尾”)—数组是初始化为该类型的默认值。)
如果您要从零的数组转到根本没有零的数组,则必须使用两次遍历-一次计算非零,构造一个适当大小的新数组,然后复制非零零值以上。 也可以反向进行(压缩初始数组,然后将其“填充”部分复制过来),但是稍微复杂一些。
如果您继续使用当前方法(结果数组将为零,但最后),则需要维护两个索引指针-一个是主循环迭代器,第二个是放置非索引指针的下一个位置零值 ,仅当复制值(或不移动,如两个索引相同,直到遇到第一个0时才会如此)时才递增。 确保将非零移动的位置“零”。 如果订单没有需要保留可以减少移动次数。
import java.util.Arrays;
import java.util.Scanner;
public class StackOverflow1
{
public static final double KEY = 0.0;
private static final Scanner INPUT = new Scanner(System.in);
public static void main(String[] args) {
int length = INPUT.nextInt();
double[] array = new double[length];
for(int i=0; i<length; i++) {
array[i] = INPUT.nextDouble();
}
int index = 0;
for(int k = 0; k < length ; k++) {
if(array[k] == KEY) {
continue;
}
array[index] = array[k]; // bring the non-zeroth element forward
if (index != k) array[k] = 0; //make the non-zeroth element zero in the actual location
index++;
}
System.out.println("n = " + index + " array = " + Arrays.toString(array));
}
}
您的实现(第二个for循环)不正确,它将无法通过简单的测试用例:Input> 5 2.0 2 0.0 3 0.0您的程序将具有错误的输出:2.0 2.0 3.0 3.0
但应该是2.0 2.0 3
另外,您不能使用==比较两个double。
以下代码是基于您当前代码的我的解决方案:
public class hw21 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
final double KEY = 0.0;
final Double ACCEPTABLE_TOLERANCE = 0.000000000001d;
int n = scan.nextInt();
double[] a = new double[n];
for (int i = 0; i < n; i++) {
a[i] = scan.nextDouble();
}
for (int k = 0, j = 0; k < n; k++) {
if (Math.abs(a[k] - KEY) < ACCEPTABLE_TOLERANCE) {
continue;
}
a[j] = a[k];
System.out.println(a[j]);
j++;
}
}
}
我也更喜欢使用如下的ArrayList:
public class hw2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
final double KEY = 0.0;
final Double ACCEPTABLE_TOLERANCE = 0.000000000001d;
int n = scan.nextInt();
double[] a = new double[n];
for (int i = 0; i < n; i++) {
a[i] = scan.nextDouble();
}
List<Double> newList = new ArrayList<Double>();
for (int k = 0; k < n; k++) {
if (Math.abs(a[k] - KEY) < ACCEPTABLE_TOLERANCE) {
continue;
}
newList.add(a[k]);
}
System.out.println("There are " + newList.size() + " no-zero double:");
System.out.println(newList);
}
}
您可以按以下方式删除那些不需要的零,但在这种情况下,它将得到排序。
@org.junit.Test
public void test15() throws Exception {
double[] arr = new double[]{0.0,1.1,0.1,0.0,2.1};
double[] nonZeroArr = arr;
Arrays.sort(nonZeroArr);
int index = -1;
while((index = Arrays.binarySearch(nonZeroArr, 0.0)) > -1){
double[] newArr = new double[nonZeroArr.length-index-1];
System.arraycopy(nonZeroArr, index+1, newArr, 0, newArr.length);
nonZeroArr = newArr;
}
for (double d : arr) {
System.out.print(d +",");
}
System.out.println();
for (double d : nonZeroArr) {
System.out.print(d + ",");
}
}
如果您想在课堂上听起来真的很聪明,请指出这是许多语言中唯一的一种,比Java生产力更高:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.