In Java, The teacher taught us how to remove an element from an array without using array utils and so on. So I tried the method he gave to us. It updates the index value exactly as I want. after it changes the index value, I want it to delete the last index, "sizeOfArray-1" but I couldn't do that! Any help?
Here the code:
import java.util.Scanner;
public class Arrays {
static int x[] = { 1, 2, 3, 4, 5, 6, 7 };
static Scanner input = new Scanner(System.in);
public int search(int target) {
for (int index = 0; index < x.length; index++) {
if (x[index] == target)
return index;
}
return -1;
}
public void deleteIndex(int target) {
int deleted = search(target);
if (deleted == -1)
System.out.println("Entry Not Found!");
else {
x[target] = x[7-1];
}
}
public static void main(String[] args) {
Arrays f = new Arrays();
int counteri = 0;
int counterj = 0;
for (int j = 0; j < x.length; j++) {
System.out.print(counterj + "=>" + x[j] + " \n");
counterj++;
}
f.deleteIndex(input.nextInt());
for (int i = 0; i < x.length; i++) {
System.out.print(counteri + "=>" + x[i] + " \n");
counteri++;
}
}
}
Arraysalready exists.