Number and appearance in arrays

My homework is the implementation of the package using arrays, where you can add and remove numbers as you please. So far I have managed to do everything except a list of numbers, and it should look like this:

img

I don’t know where to start, so some pointers will be nice, please and thanks. (note: I am not allowed to use anything other than arrays, so arraylist, collection, etc. cannot help me.)

EDIT: I created another array for counting, and in some cases the code works fine. However, sometimes when I run it, it gives me a completely wrong result, and I don’t know how to fix it? For example, if I put the numbers (in this order) 11, 22, 11, 33, 11, 22, I will go back:

enter image description here

import java.util.Scanner;

public class Bag {

int index = 0;
int[] array = new int[50];

public static void main(String[] args) {
    int x = 0;
    Bag bag = new Bag();

    Scanner scan = new Scanner(System.in);

    while (x == 0) {
        System.out.print("Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> ");
        char c = scan.next().charAt(0);
        switch (c) {
            case 'A':
                int y = scan.nextInt();
                bag.Add(y);
                break;
            case 'D':
                int d = scan.nextInt();
                bag.Delete(d);
                break;
            case 'F':
                int z = scan.nextInt();
                bag.Find(z);
                break;
            case 'S':
                bag.Size();
                break;
            case 'm':
                bag.Min();
                break;
            case 'M':
                bag.Max();
                break;
           case 'L': bag.List();
                break;
            case 'Q': bag.Quit();
            }
        }

}

public void Add(int y) {
    array[index] = y;
    index++;
    System.out.println("   " + y + " is added to the Bag. ");
}

    public void Delete(int d) {
for (int i = 0; i < index; i++) {
    if (d == array[i]) {
        while (i < index) {
                array[i] = array[i + 1];
            i++;
        }
        System.out.println("   " + d + " is deleted from the Bag.");
        index--;
        return;
    }
}
System.out.println("   Cannot delete " + d
        + ". It does not exist in the Bag.");

    }   

public void Find(int z)
   {
     int count = 0;
     for (int i = 0; i < array.length; i++) {
       if (array[i] == z) {
          count++;
        }
      }
      System.out.println("   There is (" + count + ") " + z
                + " in the Bag.");

          }
public void Size() {
    System.out.println("  There are " + index + " numbers in the Bag.");
}

public void Min() {
    int min = array[0];
    for (int i = 1; i < index; i++) {
        if(min > array[i]) {
            min = array[i];
        }
    }
    System.out.println("   The minimum number in the Bag is " + min + ".");
}
public void Max() {
    int max = array[0];
    for (int i = 1; i < index; i++) {
        if(max < array[i]) {
            max = array[i];
        }
    }
    System.out.println("   The maximum number in the Bag is " + max + ".");
}
public void Quit() {
    System.out.println("Bye…");
}

public int Count(int c) {
    int elements = 0;
    for (int i = 0; i < array.length; i++) {
        if (array[i] == c) {
            elements++;
        }
    }
    return elements;
}
public void delete(int c) {
    for (int i = 0; i < index; i++) {
        if (c == array[i]) {
            while (i < index) {
                array[i] = array[i + 1];
                i++;
            }
            index--;
            return;
        }
    }
}
public void List() {
    System.out.println("+--------+--------+");
    System.out.println("| Number | Occurs |");
    System.out.println("+--------+--------+");

    for(int i = 0; i < index; i++){
        if(Count(array[i]) == 1){

      System.out.printf("|%8d|%8d|\n", array[i], Count(array[i]));
      System.out.println("+--------+--------+");
        }
        else{
            System.out.printf("|%8d|%8d|\n", array[i], Count(array[i]));
            System.out.println("+--------+--------+");
            for(int j = 0; j <= Count(array[i]); j++){
                delete(array[i]);
            }
        }
    }
}
}
+4
2

, List(). :

public void List() {
System.out.println("+--------+--------+");
System.out.println("| Number | Occurs |");
System.out.println("+--------+--------+");

while(index > 0){
    int del = array[0];
    int count = Count(del);
    System.out.printf("|%8d|%8d|\n", del, count);
    System.out.println("+--------+--------+");
    for(int j = 0; j < count; j++){
        delete(del);
    }
}

}

+--------+--------+
| Number | Occurs |
+--------+--------+
|      11|       3|
+--------+--------+
|      22|       2|
+--------+--------+
|      33|       1|
+--------+--------+

array.length , 0.

public int Count(int c) {
    int elements = 0;
    for (int i = 0; i < index; i++) {
        if (array[i] == c) {
            elements++;
        }
    }
    return elements;
}
0

:

public int count(int c) {
    int count = 0;
    for (int i = 0; i < index; i++)
        count += (c == array[i]) ? 1 : 0; 
    return count;
}

public void list() {
    System.out.println("+--------+--------+");
    System.out.println("| Number | Occurs |");
    System.out.println("+--------+--------+");
    printRec(array);
}

private void printRec(int...ints) {
    int constant = ints[0];
    if (constant == 0) return;
    int count = count(constant);
    System.out.printf("|%8d|%8d|\n", constant, count);
    System.out.println("+--------+--------+");
    for (int i = 0; i < count; i++)
        delete(constant);
    printRec(array);
}

, void bag.list(). . mehod , array, .

, lowercase. , .

0

Source: https://habr.com/ru/post/1611679/


All Articles