创建一个数组和一个允许从数组中删除零的函数
create an array and a function allowing deletion zeros rom the array
我有一个代码可以创建一个包含 7 个整数的数组,而系统有一个函数可以检查 array[i] == 0 将在数组的最右侧放置零,最后系统显示像这样的原始数组和压缩数组
原始数组:
0 5 0 12 0 0 4
压缩数组:
5 12 4 0 0 0 0
谁能帮帮我??
这是代码
package question3;
import java.util.Scanner;
public class ArrayWithCompression {
static int[] array = new int[7];
static Scanner sc = new Scanner(System.in);
public static void main(String[] args){
System.out.println("enter numbers");
for(int i = 0 ; i< array.length; i++){
array[i] = sc.nextInt();
}
System.out.println(compression(array));
}
public static int[] compression(int[] newarray){
for(int i = 0; i< array.length; i++ ){
if(array[i] == 0){
array[i] = i++;
}
array[i] = newarray[i];
}
return newarray;
}
}
您接近正确答案了。我建议您在调试器中单步执行代码,看看您在做什么。
既然不给你答案就很难给你提示...
public static int[] compression(int[] array){
int j = 0;
// iterate over all the numbers in the array.
for (int i : array)
// if the number is not 0 add it back into the array
if (i != 0)
// add it back only counting non-0 for the index
array[j++] = i;
// fill the rest of the array with zero.
Arrays.fill(array, j, array.length, 0);
}
您可以在您的压缩方法中尝试此代码:
public static int[] compression(int[] newarray){
int []arr= new int[newarray.length];
int j=0;
for(int i = 0; i< newarray.length; i++ ){
if(newarray[i] != 0){
arr[j++] = newarray[i];
}
}
return arr;
}
不使用其他数组:
public static int[] compression(int[] newarray){
for(int i = 0; i< newarray.length; i++ ){
if(newarray[i] != 0){
continue;
}else
{
for(int j = i; j< newarray.length; j++)
{
if(newarray[j] != 0)
{
newarray[i]=newarray[j];
newarray[j]= 0;
break;
}
}
}
}
return newarray;
}
你可以改变你的主要方法(打印数组):
array= compression(array);
for(int i = 0 ; i< array.length; i++){
System.out.println(array[i]);
}
您可以尝试一下:
for (int i = 0; i < arr.length - i; i++) {// loop decreases as after each loop a zero if found is put at last
for (int j = 0; j < arr.length - 1; j++) { // this loop puts first 0 in last by swapping each nos.
if (arr[j] == 0) {
arr[j] = arr[j + 1];
arr[j + 1] = 0;
}
}
}
return arr;
在主函数中:
array = compression(array);
for (int i : array) {
System.out.println(i);
}
我有一个代码可以创建一个包含 7 个整数的数组,而系统有一个函数可以检查 array[i] == 0 将在数组的最右侧放置零,最后系统显示像这样的原始数组和压缩数组
原始数组:
0 5 0 12 0 0 4
压缩数组:
5 12 4 0 0 0 0
谁能帮帮我??
这是代码
package question3;
import java.util.Scanner;
public class ArrayWithCompression {
static int[] array = new int[7];
static Scanner sc = new Scanner(System.in);
public static void main(String[] args){
System.out.println("enter numbers");
for(int i = 0 ; i< array.length; i++){
array[i] = sc.nextInt();
}
System.out.println(compression(array));
}
public static int[] compression(int[] newarray){
for(int i = 0; i< array.length; i++ ){
if(array[i] == 0){
array[i] = i++;
}
array[i] = newarray[i];
}
return newarray;
}
}
您接近正确答案了。我建议您在调试器中单步执行代码,看看您在做什么。
既然不给你答案就很难给你提示...
public static int[] compression(int[] array){
int j = 0;
// iterate over all the numbers in the array.
for (int i : array)
// if the number is not 0 add it back into the array
if (i != 0)
// add it back only counting non-0 for the index
array[j++] = i;
// fill the rest of the array with zero.
Arrays.fill(array, j, array.length, 0);
}
您可以在您的压缩方法中尝试此代码:
public static int[] compression(int[] newarray){
int []arr= new int[newarray.length];
int j=0;
for(int i = 0; i< newarray.length; i++ ){
if(newarray[i] != 0){
arr[j++] = newarray[i];
}
}
return arr;
}
不使用其他数组:
public static int[] compression(int[] newarray){
for(int i = 0; i< newarray.length; i++ ){
if(newarray[i] != 0){
continue;
}else
{
for(int j = i; j< newarray.length; j++)
{
if(newarray[j] != 0)
{
newarray[i]=newarray[j];
newarray[j]= 0;
break;
}
}
}
}
return newarray;
}
你可以改变你的主要方法(打印数组):
array= compression(array);
for(int i = 0 ; i< array.length; i++){
System.out.println(array[i]);
}
您可以尝试一下:
for (int i = 0; i < arr.length - i; i++) {// loop decreases as after each loop a zero if found is put at last
for (int j = 0; j < arr.length - 1; j++) { // this loop puts first 0 in last by swapping each nos.
if (arr[j] == 0) {
arr[j] = arr[j + 1];
arr[j + 1] = 0;
}
}
}
return arr;
在主函数中:
array = compression(array);
for (int i : array) {
System.out.println(i);
}