20162311 編寫Android程序測試查找排序算法

20162311 編寫Android程序測試查找排序算法

1、設置圖形界面

由於是測試查找和排序算法,因此先要有一個目標數組。爲了獲得一個目標數組,我設置一個EditText和一個Button來添加數據android

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:orientation="vertical">

        <EditText
            android:id="@+id/addData"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="輸入要添加的數據"></EditText>

        <Button
            android:id="@+id/add"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="添加數據" />

        <TextView
            android:id="@+id/dataArray"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""/>


    </LinearLayout>

採用垂直線性佈局,EditText添加一個hint屬性,提示用戶添加數據,而後點擊按鈕便可把數據添加到數組中,後面的TextView用來顯示數組中的元素。接下來又是一個EditText,用來讓客戶輸入要查找的對象;以後是兩個按鈕,一個是查找,點擊以後能夠在目標數組中進行查找,一個是排序,點擊以後會對目標數組進行排序,並把排序結果顯示在後面的TextView中算法

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <EditText
            android:id="@+id/target"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="輸入要查找的目標"></EditText>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/search"
                android:layout_width="193dp"
                android:layout_height="wrap_content"
                android:text="查找" />

            <Button
                android:id="@+id/sort"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="排序" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:id="@+id/searchText"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:hint=""/>

            <TextView
                android:id="@+id/sortText"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:hint=""/>

        </LinearLayout>
    </LinearLayout>

總體的佈局採用了線性佈局,其中又嵌套了線性佈局,利用weight這個屬性合理的分配空間。最後的結果以下數組

2、編寫MainActivity

由於要用到以前實現的查找排序算法和一些相關的類,因此先把他們從IEDA中複製到Android項目裏來ide

  • 設置變量,找到對應控件的id
private EditText addData,target;
private TextView dataArray,searchText,sortText;
private Button add,search,sort;
addData = (EditText) findViewById(R.id.addData);
target = (EditText) findViewById(R.id.target);
dataArray = (TextView)findViewById(R.id.dataArray);
searchText = (TextView)findViewById(R.id.searchText);
sortText = (TextView)findViewById(R.id.sortText);
add = (Button)findViewById(R.id.add);
search = (Button)findViewById(R.id.search);
sort = (Button)findViewById(R.id.sort);
  • 給對應的按鈕設置監聽器

首先是add按鈕,它的功能是把第一個EditText中輸入的數據存入數組中,而後把數組中的數據顯示在下面的TextView中。其中start是一個整型變量,初始值爲0,每添加一個元素自加一佈局

add.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Integer dataText = Integer.getInteger(addData.getText().toString());
                data[start] = dataText;
                start++;
                String result = "";
                for(int i:data)
                    result += i+" ";
                
                dataArray.setText(result);
            }
        });

而後設置search按鈕,它的功能是接收第二個EditText中的數據,做爲查找目標,而後調用查找方法進行查找,並把結果顯示在下面的TextView中。我這裏是以數表查找爲例測試

search.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                int targetext = Integer.getInteger(target.getText().toString());
                int result = NewSearching.treeSearch(data,targetext);
                searchText.setText("查找結果爲:"+ result);
            }
        });

最後是sort按鈕,用來對目標數組進行排序,而後把排序結果顯示在下面的TextView中,我這裏以二叉樹排序爲例3d

sort.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                NewSorting.binaryTreeSort(data);
                String result = "排序結果:\n";
                for (int s: data)
                    result += s+"\n";
                sortText.setText(result);
            }
        });

3、測試結果截圖

相關文章
相關標籤/搜索