2014-01-12 47 views
-1

我正在BlueJ中創建一個允許用戶對無序數組進行排序和搜索的應用程序。我有搜索工作。目前它要求用戶輸入一個數字來搜索數組,並返回找到或沒有找到的罰款。搜索項目的數組。如何顯示位置項目?

我希望能夠告訴用戶在數組中發現了什麼位置的數字?

下面是我的代碼爲我的搜索方法:

public static void dosearch(OArray a) 
    { 
     clrscr();    
     if (a.isEmpty()) { 
      System.out.println("Array is Empty!"); 
      pressKey(); 
      return; 
     } 
     clrscr(); 
     System.out.println("Enter number to search for : "); 
     int item; 
     item = Genio.getInteger(); 
     if (a.retrieve(item) == false) 
      System.out.println("Cannot find " + item); 
     else 
      System.out.println(item + " Found"); 
     pressKey(); 
    } 

OArray類代碼:

public class OArray extends Array 
    { 
// These are the Fields 

// Constructor 
public OArray(){ 
    super(); 
    System.out.println("OArray Created!!! size 10");     
} 

public OArray(int newsize){  
    super(newsize); 
    System.out.println("OArray Created!!!");     
} 


public boolean addToEnd(int item) 
{ 
    if (isFull() == true) 
     return false; 

    array[nextfree]=item; 
    nextfree++; 
    //bubbleSort(); 
    return true; 
}  



public void bubbleSort() 
{ 
    int temp = 0;boolean swaps = true;int last = nextfree-1;int i = 0; 

    while (swaps == true) 
    { 
     swaps=false; 
     i = 0; 
     while (i < last) 
     { 
      if (array[i] > array[i+1]) 
      { 
       temp = array[i+1]; 
       array[i+1] = array[i]; 
       array[i] = temp; 
       swaps=true; 
      } 
      i++; 
     }    
    }       
} 


public boolean retrieve(int item) 
{     
    if (isEmpty()) 
     return false;       
    int i=0; 
    while (i < nextfree) 
    { 
     if (array[i] >= item) 
     { 
      posfound=i; 
      if (item == array[i]) 
      { 
       itemback = item; 
       posfound = i; 
       return true; 
      } 
      else return false; 
     } 
     i++;    
    } 
    posfound = nextfree;   
    return false;   
} 

public boolean addToFront(int item) 
{ 
    return addToEnd(item); 
} 
+4

您正在使用非標準API。我們不知道OArray是什麼。我們所能說的只是:閱讀它的API文檔。 –

+0

我想知道什麼是OArray,在google中找不到。 – Buddha

+0

對不起,我是新手。 OArray是包含數組使用的一些方法的類。包含我發佈的代碼的菜單類繼承了OArray類中的方法。這有任何意義嗎?道歉。 –

回答

0

一般來說,在一個數組來訪問某個項目的唯一途徑是通過指數,因此,瞭解索引很容易,因爲你已經知道了。傳統的習慣用法是,你有某種find()方法,如果找到它就返回元素的索引,否則返回-1

int[] a = new int[] { 7, 2, 4, 6 }; 
int foundAt = findItemInArray(a, 2); 
if (foundAt >= 0) { 
    System.out.println("Found at [" + foundAt + "]"); 
} else { 
    System.out.println("Not found"); 
} 

public static int findItemInArray(int[] a, int lookingFor) { 
    for (int i = 0; i < a.length; i++) { 
     if (a[i] == lookingFor) { 
      return i; 
     } 
    } 
    return -1; 
} 
0

您可以使用線性搜索方法。 在for循環存儲中找到數字後,它就是索引。您可以稍後再顯示它。

+0

這不是一個完整的解決方案。這可能只是一個評論。一旦你有足夠的[聲譽](http://stackoverflow.com/help/whats-reputation),你將能夠[評論任何職位](http://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 – Raju

+0

好吧,我會刪除它 – user23068