所以我想在数组中输出某个值的位置。如何输出数组值的位置? (代码后的例子):
public class Journal5b {
public int[] [] createArray (int rSize , int cSize)
{
int [] [] array = new int [rSize][cSize];
Random r = new Random();
for (int[] array1 : array) {
for (int column = 0; column < array[0].length; column++) {
array1[column] = r.nextInt(26);
}
}
return array;
}
public void print2DArray (int [] [] array)
{
for (int[] array1 : array) {
for (int column = 0; column < array[0].length; column++) {
System.out.print(array1[column] + "\t");
}
System.out.println("\n");
}
}
public int countInstance (int [] [] array, int searchForNum)
{
int count = 0;
for (int row = 0; row < array.length; row++)
{
for (int column = 0; column < array[0].length; column++) {
if (array[row][column] == searchForNum) {
count++;
}
}
}
return count;
}
public static void main(String[] args)
{
Scanner in = new Scanner (System.in);
int [] [] myArray;
Journal5b j5b = new Journal5b();
myArray = j5b.createArray(10, 10);
j5b.print2DArray(myArray);
int val;
System.out.println("Enter the number to search for ");
val = in.nextInt();
System.out.println("Your Number popped up " + j5b.countInstance(myArray, val) + " times");
}
}
所以输出我看起来像这样:
22 20 12 25 10 8 3 4 12 25
11 24 0 7 3 21 23 9 23 11
22 18 10 24 11 13 0 25 8 18
7 14 0 5 11 12 9 22 0 16
14 11 12 4 16 3 20 22 18 25
8 24 12 6 25 4 3 16 10 23
23 11 8 12 19 15 3 25 12 6
10 3 5 22 11 7 0 7 4 4
18 1 14 23 7 13 9 9 12 9
20 10 6 14 13 1 9 15 0 3
Enter the number to search for
10
Your number popped up 5 times and is located on row 0,2,5,7, 9
如果有意义,我如何输出我想输出的内容...我正在尝试添加到countInstance并找到每行输入的特定数字的位置。
我已经完成了大部分代码,但是我已经使用了这部分代码。我无法找到下一步。我想添加is located on row 0,2,5,7, 9
答案 0 :(得分:1)
是的,我现在明白了, 对不起,我不得不完全重构你的代码。
public class Journal5b {
private int[] rowSpotted;//variable to keep rows of searched number
private int[][] arrayToBeSearched;
public class Journal5b (int rows, int columns) {
rowSpotted = new int[rows];
arrayToBeSearched = new int[rows][columns];
this.createArray();
this.print2dArray();
}
private void createArray() {
Random r = new Random();
for (int[] array1 : arrayToBeSearched) {
for (int number: array1) {
number = r.nextInt(26);
}
}
}
//I removed the array local variable array so that you won't have to keep passing it as an argument, It's now an instance variable.
//You can also use a for each loop to populate the inner array, no need to use the for loop.
public void print2DArray () {
for (int[] array1 : arrayToBeSearched) {
for (int num: array1) {
System.out.print(num + "\t");
}
System.out.println("\n");
}
}
//just prints the array to be searched.
public int countInstance (int searchForNum) {
int count = 0;
for (int row = 0; row < arrayToBeSearched.length; row++) {
for (int column = 0; column < arrayToBeSearched[0].length; column++) {
if (arrayToBeSearched[row][column] == searchForNum) {
rowSpotted[count] = row; //if the number is found put the row number into the row
count++;
}
}
}
return count;
}
public void printRowSpotted() {
for(int num : rowSpotted ) {
System.out.print(num + ',');
}
}
public static void main(String[] args) {
//scanner stuff......
Journal5b jb = new Journal5b(10,10);
//get number to be searched for....
System.out.print("Your number popped up " + jb.countInstance() + ", on rows " + jb.printRowSpotted());
}
答案 1 :(得分:0)
您可以使用#include <list>
#include <deque>
#include <iostream>
using namespace std;
template<typename T> ostream & print(const T & start, const T & end)
{
T tmp = start;
for(; tmp != end; ++tmp)
{
cout<< *tmp<< " ";
}
return cout;
}
class A
{
public:
int a;
public:
A(int a):a(a) {}
A(const A & a) {}
};
ostream & operator<<(ostream & c, const A & o)
{
c<<o.a;
return c;
}
int main()
{
int tab[]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
list<A> l1(tab, tab+10);
deque<A> d1;
list<A>::iterator it;
for(it = l1.begin(); it != l1.end(); ++it)
{
d1.insert(d1.begin(), it[0]);
}
print(d1.begin(), d1.end())<<endl;
return 0;
}
从ArrayList
返回值,而不是使用全局变量。
countInstance
并稍微修改public ArrayList<Integer> countInstance(int[][] array, int searchForNum) {
//int count = 0;
//Use an ArrayList to get the row index
ArrayList<Integer> countList = new ArrayList<>();
for (int row = 0; row < array.length; row++) {
for (int column = 0; column < array[0].length; column++) {
if (array[row][column] == searchForNum) {
//count++;
//add row index to the ArrayList count
countList.add(row);
}
}
}
return countList;
}
方法以打印所需的结果
main()