0

If I have an array type array[x][y] and for x=1 I have 3 different values for y. How do I delete 1 value from y and maybe restructure the array?

package javaphone;

import java.util.Arrays;
import java.util.Scanner;

public class DeleteContract {
    public static void DeleteContract() {
        Scanner input = new Scanner(System.in);
        System.out.println("Type your VAT number: ");
        String vat = input.next();
        int index;
        boolean result = false;
        int i = 0;
        while (!result && i < NewCustomer.counter) {
            result = (NewCustomer.vats[i].equals(vat));
            i++;
        }
        if (result) {
            index = i;
            System.out.println("--------Existing Contracts--------");
            for (int y = 0; y < NewContract.contractCounter; y++) {
                System.out.println("Number: " + NewContract.number[i][y]
                        + "Starting Date: " + NewContract.startdate[i][y]
                        + "Lenght: " + NewContract.length[i][y]);
            }
            System.out.println("Which contract you wish to delete?");
            int del = input.nextInt();
        } else {
            System.out.println("There's no registered customer with this VAT.");
            return;
        }
    }
}

This is my code but I didn't want to scare you guys with unknown values to you, but if you understand my code, what I am basically trying to do is delete a contract from a user, where users are basically x and their contracts are y, as you see in my code, every contract is defined by 3 values number, startdate and lenght

1

3 Answers 3

1

You can not delete elements from old 2d array types, what you can do is just replace matrix with ArrayLists ArrayList<ArrayList<Object>> and than you can have each row different, eg. one with 5 elements one with 3, and delete them as you wish.

Sign up to request clarification or add additional context in comments.

Comments

0

You can remove a value and restructure a 2d array as follows:

int[][] arr = {
        {10, 20, 30},
        {10, 20, 30},
        {10, 20, 34}};
int[][] arr2 = Arrays.stream(arr)
        .map(row -> Arrays.stream(row)
                .filter(val -> val != 34)
                .toArray())
        .toArray(int[][]::new);
// output
Arrays.stream(arr2).map(Arrays::toString).forEach(System.out::println);
//[10, 20, 30]
//[10, 20, 30]
//[10, 20]

Comments

0

You can process the rows of the array using setAll method: filter out unnecessary row elements and replace the row if necessary:

int[][] arr = {
        {10, 20, 30},
        {10, 20, 30},
        {10, 20, 35}};
// process the rows of the array
Arrays.setAll(arr, i -> {
    int[] nRow = Arrays.stream(arr[i])
            // filter out unnecessary elements
            .filter(val -> val != 35)
            // return the new row
            .toArray();
    // replace the current row if the new row is shorter
    return nRow.length < arr[i].length ? nRow : arr[i];
});
// output
Arrays.stream(arr).map(Arrays::toString).forEach(System.out::println);
//[10, 20, 30]
//[10, 20, 30]
//[10, 20]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.