无法在此阵列程序中执行插入和删除操作。请给出建议。只有logger.error(s"Trace: %s".format(t.getStackTrace.mkString("\n")))
正在运作。
我尝试使用temp []复制Arr [],然后将Arr []引用到temp []
完整代码为here
logger.error(s"Trace: %s".format(t.getStackTrace.mkString(" -> ")))
答案 0 :(得分:0)
你的代码有不同的失败点,不确定我是否会抓住它们:
for (int y=0; y<Arr.length; y++)
。使用for (int y :Arr)
,您可以将y分配给Arr的所有元素。希望这有点帮助...
答案 1 :(得分:0)
这是代码::你以错误的方式使用了扩展for循环。您应该首先看到for循环的扩展是如何工作的链接:https://www.cis.upenn.edu/~matuszek/General/JavaSyntax/enhanced-for-loops.html
import java.util.Arrays;
class GfG {
public static void main(String[] args) {
System.out.println();
int[] Arr = { 1, 2, 3, 4 };
insert(2, Arr);
Arr = delete(30, Arr);
System.out.println(Arrays.toString(Arr));
}
public static boolean find(int x, int Arr[]) {
for (int y = 0; y < Arr.length; y++) {
if (Arr[y] == x) {
System.out.println(" Found , At Position : " + y);
return true;
}
}
System.out.println(" Not Found");
return false;
}
public static int[] delete(int x, int Arr[]) {
int [] temp = Arr;
for (int i = 0; i < Arr.length; i++) {
if (Arr[i] == x) {
for (int j = i; j < Arr.length - 1; j++) {
Arr[j] = Arr[j + 1];
}
temp = Arrays.copyOfRange(Arr, 0, Arr.length-1);
break;
}
}
return temp;
}
public static void insert(int x, int Arr[]) {
int temp[] = new int[Arr.length + 1];
for (int y = 0; y < Arr.length; y++) {
temp[y] = Arr[y];
}
temp[Arr.length] = x;
Arr = temp;
}
}
答案 2 :(得分:0)
我用过这个approch。
import java.io.*;
import java.util.*;
interface ArrayOperation {
boolean find(int x,int Arr[]);
int[] delete(int x,int Arr[]);
int[] insert(int x,int Arr[]);
}
class ArrayExample implements ArrayOperation {
public boolean find(int x,int Arr[]){
for (int y=0;y<Arr.length;y++)
{
if(Arr[y]==x) {
System.out.println(" Found , At Position : " + y);
return true;
}
}
System.out.println(" Not Found");
return false;}
public int[] delete (int x,int Brr[]){
int temp[]=new int[Brr.length-1];
for (int y=0;y<Brr.length;y++){
if(Brr[y]==x){
for (int n=y;n<Brr.length-1;n++){
Brr[n]=Brr[n+1];
System.out.println("Deleted.");
}
for (int k=0;k<temp.length;k++){
temp[k]=Brr[k];
}
}
}
return temp;
}
public int [] insert(int x,int Brr[]){
int temp[]=new int[Brr.length + 1];
for (int y=0;y<Brr.length;y++){
temp[y]=Brr[y];
}
temp[Brr.length]=x;
return temp;
}
}
public class ArrayCreateFindDeleteInsert{
public static void main (String args[]){
System.out.println("Enter the size of Array :");
Scanner in = new Scanner(System.in);
int x=in.nextInt();
System.out.println("You Choose Size : " + x);
int Arr[]=new int[x];
ArrayExample A = new ArrayExample();
System.out.println("Enter Elements");
for(int y=0;y<Arr.length;y++){
int z=in.nextInt();
Arr[y]=z;
}
while(true){
System.out.print("Your Array : ");
for(int y=0;y<Arr.length;y++){
System.out.print(Arr[y]+" ");
}
System.out.println("\nSelect an operation : ");
System.out.println("1. Find : ");
System.out.println("2. Delete : ");
System.out.println("3. Insert : ");
System.out.println("4. Quit : ");
int p=in.nextInt();
switch(p){
case 1: System.out.print("Enter Element: ");
int t=in.nextInt();
System.out.print(t);
A.find(t,Arr);
break ;
case 2: System.out.print("Enter Element: ");
int u=in.nextInt();
while(A.find(u,Arr)==true){
Arr=A.delete(u,Arr);
}
break ;
case 3: System.out.print("Enter Element: ");
int v=in.nextInt();
System.out.print(v);
Arr=A.insert(v,Arr);
System.out.print(" Inserted.");
break ;
case 4:
System.exit(0);
}
}
}
}