2017-02-17 5 views
1

Ich versuche ein Programm zu erstellen, mit dem Sie herausfinden können, wie oft eine Curtain-Nummer in einem 2D-Array auftritt. Eine der Sachen, die ich tun muss, ist, eine Methode namens countInstence zu erstellen, die die Häufigkeit zählt, wie oft eine Zahl auftritt, aber auch, wo sie sich im Array befinden. das Problem, das ich habe, ist, wie die Position der der Nummer I-Ausgang SteckenSuchen der Position einer Nummer in einem 2d-Array

public int[][] createArray(int rSize, int cSize) { 

    Random rnd = new Random(); 
    int[][] array = new int[rSize][cSize]; 

    for (int row = 0; row < array.length; row++) { 
     for (int col = 0; col < array[0].length; col++) { 
      array[row][col] = rnd.nextInt(26); 
     } 
    } 
    return array; 
} 

public void print2dArray(int[][] array) { 

    for (int row = 0; row < array.length; row++) { 
     for (int col = 0; col < array[0].length; col++) { 
      System.out.print(array[row][col] + "\t"); 
     } 
     System.out.println("\n"); 
    } 
} 

public int countInstence(int[][] array, int search) { 

    int count = 0; 

    for (int row = 0; row < array.length; row++) { 
     for (int col = 0; col < array[0].length; col++) { 
      if (array[row][col] == search) 
       count++; 

     } 
    } 
    return count; 
} 

public static void main(String[] args) { 

    Journal5b call = new Journal5b(); 
    Scanner in = new Scanner(System.in); 
    int[][] myArray; 
    int value; 
    myArray = call.createArray(10, 10); 

    call.print2dArray(myArray); 

    System.out.println("Enter a number to search for: "); 
    value = in.nextInt(); 

    System.out.println("Your number occurred " 
      + call.countInstence(myArray, value) + " Times"); 
} 

Antwort

2

nur eine einzeilige Druck Anweisung innerhalb des if-statement und Format der Zeile und Spalte des aktuellen Index, wie weiter unten drucken ...

public int countInstance(int[][] array, int search) { 

    int count = 0; 

    for (int row = 0; row < array.length; row++) { 
     for (int col = 0; col < array[0].length; col++) { 
      if (array[row][col] == search) { 
       System.out.printf("Instance found at [%d, %d]\n", row, col); 
       count++; 
      } 
     } 
    } 

    return count; 
} 

Wenn Sie nicht vertraut mit dem System.out.printf Anruf oder die %d Symbole sind, können Sie mehr darüber here lesen.

0

Fügen Sie einfach diese Zeile des for-Schleife

for (int row = 0; row < array.length; row++) { 
    for (int col = 0; col < array[0].length; col++) { 
     if (array[row][col] == search) { 
      System.out.printf("Instance found at [%d, %d]\n", row, col);// add this line 
      count++; 
     } 
    } 
} 
0

Wenn Sie auch Standort an anderer Stelle verwendet werden sollen, um dann nur eine Liste deklarieren und Standort zu dieser Liste hinzuzufügen, während für das Element zu suchen.

public static List<String> location=new ArrayList<String>(); 
    public int countInstence(int[][] array, int search) { 

    int count = 0; 

    for (int row = 0; row < array.length; row++) { 
     for (int col = 0; col < array[0].length; col++) { 
      if (array[row][col] == search) 
      { 
       count++; 
       String loc=row+","+col; 
       location.add(loc); 
      } 
     } 
    } 
    return count; 
} 

public static void main(String[] args) { 

    Test call = new Test(); 
    Scanner in = new Scanner(System.in); 
    int[][] myArray; 
    int value; 
    myArray = call.createArray(10, 10); 

    call.print2dArray(myArray); 

    System.out.println("Enter a number to search for: "); 
    value = in.nextInt(); 

    System.out.println("Your number occurred " 
      + call.countInstence(myArray, value) + " Times"); 
    System.out.println("locations :"); 
    for(int i = 0; i < location.size(); i++) { 
     System.out.println(location.get(i)); 
    } 

} 
0

Ich schlage vor, mit Punkten:

List<Point> locations=new ArrayList<Point>(); 
     .... 
     if (array[row][col] == search) 
     { 
      count++; 
      locations.add(new Point(row,col)); 
     } 
Verwandte Themen