比较字符串与冒泡排序算法

时间:2014-12-05 06:16:59

标签: java android string bubble-sort

我有一个程序可以从数据库中获取几个String(announcementId和announcementTitle),

然后对于从数据库中提取的每个字符串,我想比较它们中的每一个(比较所有获取的announcementId),

如果字符串(announcementId)具有不同的值,那么它将使用announcementTitle创建一个新的Button,因为它从数据库获取(按钮)值。

我试着学习如何,并发现在这种情况下可以使用冒泡排序算法,但程序中存在一些错误。索引超出范围的异常.. 然后我尝试了一些代码并尝试更改数组,但它仍然没有用。

请您查看我的代码并告诉我错误的位置以及解决错误的最佳方法

这是我的代码:

    myDb = new Database(ViewAllAnnouncement.this);
    myDb.open();
    totalAnnouncement = myDb.countHowManyAnnouncement(username);
    String temp = Integer.toString(totalAnnouncement);
    //Toast.makeText(getApplicationContext(), temp, Toast.LENGTH_LONG).show();

    String[] announcementTitle = myDb.fetchMyAnnouncement(username);
    String[] announcementId = myDb.fetchAnnouncementId(username);

    for (int i = 0; i < totalAnnouncement; i++) {

        for (int j = 0; j < totalAnnouncement - i; j++) {
            if (j > 0 || j < totalAnnouncement-i) {
                if (!announcementId[j].equals(announcementId[i])) {

                    newBt = new Button(this);
                    newBt.setTag(announcementId[i]);
                    newBt.setText(announcementTitle[i]);
                    newBt.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            Button mButt = (Button) v;
                            String temp = (String) mButt.getTag();
                            Intent intent = new Intent(
                                    "com.example.teamizer.VIEWCHILDANNOUNCEMENT");
                            intent.putExtra("annId", temp);
                            startActivity(intent);
                        }
                    });
                    layout.addView(newBt);
                }
            }
        }
    }

    myDb.close();

这是我返回announcementId的方法

    public String[] fetchAnnouncementId(String Username) {
    // TODO Auto-generated method stub
    int i = 0;

    String Query = "SELECT b." + ANNOUNCEMENT_ID + " FROM "
            + MS_GROUP_DETAIL + " a, " + MS_ANNOUNCEMENT_DETAIL + " b, "
            + MS_ANNOUNCEMENT + " c WHERE a." + GROUP_ID + " = b."
            + GROUP_ID + " AND b. " + ANNOUNCEMENT_ID + " = c."
            + ANNOUNCEMENT_ID + " AND a." + MEMBER_USERNAME + " =? ORDER BY b." +ANNOUNCEMENT_ID;
    Cursor c = ourDatabase.rawQuery(Query, new String[] { Username });

    String temp[] = new String[c.getCount()];
    int iArray = c.getColumnIndex(ANNOUNCEMENT_ID);

    c.moveToFirst();
    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        temp[i] = c.getString(iArray);
        i++;
    }
    c.close();
    return temp;
}

1 个答案:

答案 0 :(得分:1)

如果冒泡排序是答案,你必须误解了这个问题。我强烈建议您添加

ORDER BY announcementId

到查询结尾。这样,数据库将按您的列排序。

修改 你可以用

ORDER BY 1

按第一列排序(并省略名称)。然后,您的代码应该类似于

for (int i = 0; i < totalAnnouncement - 1;) {
    int j = i + 1;
    for (; j < totalAnnouncement; j++) {
        if (!announcementId[j].equals(announcementId[i])) {
            break;
        }
    }
    // j is the first value where the announcementId changes
    newBt = new Button(this);
    newBt.setTag(announcementId[i]);
    newBt.setText(announcementTitle[i]);
    newBt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Button mButt = (Button) v;
            String temp = (String) mButt.getTag();
            Intent intent = new Intent(
                    "com.example.teamizer.VIEWCHILDANNOUNCEMENT");
            intent.putExtra("annId", temp);
            startActivity(intent);
        }
    });
    layout.addView(newBt);
    i = j; // <-- assign j to i.
}