我正在尝试创建名为split的方法,该方法根据键将列表分为2个列表。如果list_1和list_2是结果列表,则list_1应包含原始列表的所有项目,其中密钥小于或等于传递的密钥,list_2应包含其密钥大于传递的密钥的原始列表的所有项目。到目前为止,我将发布我的代码以及其他人的建议
public class UnorderedArrayList extends ArrayListClass {
public UnorderedArrayList() {
super();
}
public UnorderedArrayList(int size) {
super(size);
}
//Bubble Sort
public void bubbleSort() {
for (int pass = 0; pass < length - 1; pass++) {
for (int i = 0; i < length - 1; i++) {
if (list[i] > list[i + 1]) {
int temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
}
}
}
}
//implementation for abstract methods defined in ArrayListClass
//unordered list --> linear search
public int search(int searchItem) {
for(int i = 0; i < length; i++)
if(list[i] == searchItem)
return i;
return -1;
}
public void insertAt(int location, int insertItem) {
if (location < 0 || location >= maxSize)
System.err.println("The position of the item to be inserted is out of range.");
else if (length >= maxSize)
System.err.println("Cannot insert in a full list.");
else {
for (int i = length; i > location; i--)
list[i] = list[i - 1]; //shift right
list[location] = insertItem;
length++;
}
}
public void insertEnd(int insertItem) {
if (length >= maxSize)
System.err.println("Cannot insert in a full list.");
else {
list[length] = insertItem;
length++;
}
}
public void replaceAt(int location, int repItem) {
if (location < 0 || location >= length)
System.err.println("The location of the item to be replaced is out of range.");
else
list[location] = repItem;
}
public void remove(int removeItem) {
int i;
if (length == 0)
System.err.println("Cannot delete from an empty list.");
else {
i = search(removeItem);
if (i != -1)
removeAt(i);
else
System.out.println("Cannot delete! The item to be deleted is not in the list.");
}
}
public void merge(UnorderedArrayList list2,UnorderedArrayList list1){
int num=0;
for(int j=0; j<list1.length;j++){
num= list1.retrieveAt(j);
insertEnd(num);
}
for(int i=0; i<list2.length-1;i++){
num=list2.retrieveAt(i);
insertEnd(num);
}
}
public void split(UnorderedArrayList list2, UnorderedArrayList list1, UnorderedArrayList list, int item){
int listItem = item;
while(!list.isEmpty()){
list.retrieveAt(listItem);
if(listItem>item){
if(!list2.isFull()){
list2.insertAt(listItem);
}
}
}
}
//what i got so far from the internet
/* void UnsortedType::SplitLists(ItemType item, UnsortedType& list1, UnsortedType& list2){
ItemType listItem;
list.ResetList();
while ( !list.IsLastItem()) {
list.GetNextItem(listItem);
if(listItem > item) {
if (!list2.IsFull())
list2.InsertItem(listItem);
}
else {
if ( !list1.IsFull())
list1.InsertItem(listItem);
} }}
*/
答案 0 :(得分:0)
您有很多代码,与您的要求无关。鉴于列表和密钥,我只想:
这就是你所需要的一切。
答案 1 :(得分:0)
有同样的问题。您需要清除已经完成的两个列表(list1和list2),以便它们可用于接收值。看看下面我编写的代码,这些代码在我的程序中可以作为指导。对于客户端中的方法调用,您的参数应为:(list_1,list_2,result,split)。您的新列表将根据其名称在客户端中提供给您。
public void split(UnorderedArrayList list1, UnorderedArrayList list2, UnorderedArrayList list3, int key) {
int num = 0;
list1.clearList();
list2.clearList();
for(int x = 0; x < list3.length; x++) {
num = list3.retrieveAt(x);
if(num <= key)
list1.insertEnd(num);
else
list2.insertEnd(num);
}
}
希望这有帮助。