gpt4 book ai didi

java - 数组按字母顺序排序

转载 作者:行者123 更新时间:2023-12-01 22:27:37 25 4
gpt4 key购买 nike

这是给出的代码:

// bubbleSort.java
// demonstrates bubble sort
// to run this program: C>java BubbleSortApp
////////////////////////////////////////////////////////////////
class ArrayBub
{
private long[] a; // ref to array a
private int nElems; // number of data items
//--------------------------------------------------------------
public ArrayBub(int max) // constructor
{
a = new long[max]; // create the array
nElems = 0; // no items yet
}
//--------------------------------------------------------------
public void insert(long value) // put element into array
{
a[nElems] = value; // insert it
nElems++; // increment size
}
//--------------------------------------------------------------
public void display() // displays array contents
{
for(int j=0; j<nElems; j++) // for each element,
System.out.print(a[j] + " "); // display it
System.out.println("");
}
//--------------------------------------------------------------
public void bubbleSort()
{
int out, in;

for(out=nElems-1; out>1; out--) // outer loop (backward)
for(in=0; in<out; in++) // inner loop (forward)
if( a[in] > a[in+1] ) // out of order?
swap(in, in+1); // swap them
} // end bubbleSort()
//--------------------------------------------------------------
private void swap(int one, int two)
{
long temp = a[one];
a[one] = a[two];
a[two] = temp;
}
//--------------------------------------------------------------
} // end class ArrayBub
////////////////////////////////////////////////////////////////
class BubbleSortApp
{
public static void main(String[] args)
{
int maxSize = 100; // array size
ArrayBub arr; // reference to array
arr = new ArrayBub(maxSize); // create the array

arr.insert(77); // insert 10 items
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);

arr.display(); // display items

arr.bubbleSort(); // bubble sort them

arr.display(); // display them again
} // end main()
} // end class BubbleSortApp
////////////////////////////

我需要更改此程序以使其插入字符串并按字母顺序显示它们。这是我到目前为止所拥有的:

class ArrayBub
{
private String[] a; // ref to array a
private String nElems; // number of data items
//--------------------------------------------------------------
public ArrayBub(int max) // constructor
{
a = new String[max]; // create the array
nElems = ""; // no items yet
}
//--------------------------------------------------------------
public void insert(String value) // put element into array
{
a[nElems] = value; // insert it
nElems++; // increment size
}
//--------------------------------------------------------------
public void display() // displays array contents
{
for(int j=0; j<nElems; j++) // for each element,
System.out.print(a[j] + " "); // display it
System.out.println("");
}
//--------------------------------------------------------------
public void bubbleSort()
{
String out;
String in;

for(out=nElems-1; out>1; out--) // outer loop (backward)
for(in=0; in<out; in++) // inner loop (forward)
if( a[in] > a[in+1] ) // out of order?
swap(in, in+1); // swap them
} // end bubbleSort()
//--------------------------------------------------------------
private void swap(String one, String two)
{
String temp = a[one];
a[one] = a[two];
a[two] = temp;
}
//--------------------------------------------------------------
} // end class ArrayBub
////////////////////////////////////////////////////////////////
class BubbleSortApp
{
public static void main(String[] args)
{
int maxSize = 100; // array size
ArrayBub arr; // reference to array
arr = new ArrayBub(maxSize); // create the array

arr.insert("hello");
arr.insert("this");
arr.insert("is");
arr.insert("a");
arr.insert("random");
arr.insert("weird ");
arr.insert("sentence");
arr.insert("that");
arr.insert("does");
arr.insert("not");
arr.insert("make");
arr.insert("any");
arr.insert("sense");

arr.display(); // display items

arr.bubbleSort(); // bubble sort them

arr.display(); // display them again
} // end main()
} // end class BubbleSortApp

我有很多错误。我很难将正确的函数从变量更改为字符串。一旦我到达代码中的某个点,我就明白了,但随后我无法将其用于其他功能等......

最佳答案

Arrays.sort(a);

这会将数组“a”按字母顺序排列

关于java - 数组按字母顺序排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28439343/

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