gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:31:17 27 4
gpt4 key购买 nike

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

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

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

我试着学习如何,发现在这种情况下可以使用冒泡排序算法,但是程序中有一些错误.. Index out of bound exception..然后我尝试了一些代码并尝试更改数组,但它仍然没有工作。

你能不能看看我的代码并告诉我错误在哪里以及修复错误的最佳方法

这是我的代码:

    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;
}

最佳答案

如果冒泡排序 是答案,您一定误解了问题。我强烈建议您添加

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.
}

关于java - 比较字符串与冒泡排序算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27309937/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com