我有编程项目我已经完成了几乎,但我正在努力将从控制台制作的新对象插入到正确位置的数组中。
我可以制作对象,插入开头或结尾,删除对象等,但数组需要按顺序(月,日,时间),我被卡住了。
我一直在通过论坛,信息网站,书籍等寻找好一天半,但找不到答案。
P.S。我不允许使用除数组(ArrayList,LinkedList等)以外的任何东西,我不能使用任何排序算法(bubblesort,insertionsort等)
方向专门为我调用根据需要移动数组元素并将新对象放在/正确位置之间。我也在使用NetBeans。
/*
*
*
*/
package project1;
import UserInput.UserInput;
public class Schedule {
Delivery[] deliObj = new Delivery[20];
int count = 0;
public Schedule(){//set default objects
deliObj[0] = new Delivery("MAR", 4, 17, 30, "Pizza");
count++;
deliObj[1] = new Delivery("APR", 1, 06, 30, "Special Deliery");
count++;
deliObj[2] = new Delivery("MAY", 6, 12, 00, "Amazon (Books)");
count++;
deliObj[3] = new Delivery("JUN", 3, 11, 15, "Car Parts");
count++;
}
public void setDelivery(Delivery[] deliObj){
this.deliObj = deliObj;
}
public Delivery[] getDelivery(){
return this.deliObj;
}
public static void main(String[] args){
Schedule scheduleObj = new Schedule();
scheduleObj.run();
}
public void run(){
System.out.println("\n***** MAIN DELIVERY CONSOLE *****\n");
System.out.println("A)dd delivery");
System.out.println("D)delete Delivery");
System.out.println("L)ist Delivery");
System.out.println("E)xit");
char selection = Character.toUpperCase(UserInput.getChar());
switch(selection){
case 'A': addDelivery();
break;
case 'D': deleteDelivery();
break;
case 'L': listDelivery();
break;
case 'E': System.exit(0);
}
}
public void addDelivery(){
Delivery getInputDelivery = new Delivery();
getInputDelivery.InputDelivery();
deliObj[count] = getInputDelivery;
count++;
insertDelivery(getInputDelivery);
run();
}
public void deleteDelivery(){
System.out.println("Please enter the number you wish to delete: ");
int num = UserInput.getInt(0,count);
//deliObj[num-1] = new Delivery();
deliObj[num-1] = null;
count--;
run();
}
public void listDelivery(){
for(int i=0;i<count;i++) {
System.out.println(i+1 + ". " + deliObj[i]);
}
run();
}
public boolean compareDelivery(Delivery A1, Delivery A2){
//Delivery delivery = deliObj[count-4];
int numMonth;
int numMonth1;
numMonth = Delivery.integerMonth(A1.getMonth());
numMonth1 = Delivery.integerMonth(A2.getMonth());
if(numMonth < numMonth1){
return true;
}
else if(numMonth == numMonth1){
if(A1.getDay() < A2.getDay()){
return true;
}
else if(A1.getDay() == A2.getDay()){
if(A1.getHour() < A2.getHour()){
return true;
}
else if(A1.getHour() == A2.getHour()){
if(A1.getMintute() < A2.getMintute()){
return true;
}
}
}
} //else return false;
return false;
}
public void insertDelivery(Delivery temp){
for(int i = 0; i < count; i++){
/*if(!compareDelivery(deliObj[i], temp)) {
for(int k = 1; k < count; k++) {
Delivery temp2 = deliObj[k];
deliObj[k] = deliObj[count-1];
deliObj[count-1] = temp2;
}*/
if(compareDelivery(deliObj[i], temp)) {
Delivery temp2 = deliObj[i];
deliObj[i] = deliObj[count-1];
deliObj[count-1] = temp2;
}
}
/*for (int k = 0; k < count-1; k++) {
if(compareDelivery(deliObj[k], temp)) {
Delivery temp2 = deliObj[k];
deliObj[k] = deliObj[count-1];
deliObj[count-1] = temp2;
//deliObj[count] = deliObj[k];
//deliObj[k] = deliObj[count-1];
}
} */
}
}
这是我运行事物的主类,但这是另一个具有构造函数和getter / setter等的子类。
package project1;
import UserInput.UserInput;
public class Delivery {
private static final String FINAL_MONTH[] = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
private String month;
private int day;
private int hour;
private int minute;
private int second;
private String userMessage;
public static int integerMonth(String compareMonth){
for(int i = 0; i < FINAL_MONTH.length; i++){
if(compareMonth.equals(FINAL_MONTH[i])){
return i;
}
}
return -1;
}
public Delivery(String month, int day, int hour, int minute, String userMessage) {
this.month = month;
this.day = day;
this.hour = hour;
this.minute = minute;
this.userMessage = userMessage;
}
public void setMonth(String month) {
for (String FINAL_MONTH1 : FINAL_MONTH) {
if (month.equalsIgnoreCase(FINAL_MONTH1)) {
this.month = month.toUpperCase();
}
}
}
public String getMonth() {
return this.month;
}
public void setDay(int day) {
this.day = day;
}
public int getDay() {
return this.day;
}
public void setHour(int hour) {
this.hour = hour;
}
public int getHour() {
return this.hour;
}
public void setMinute(int minute) {
this.minute = minute;
}
public int getMintute() {
return this.minute;
}
public void setSecond(int second) {
this.second = second;
}
public int getSecond() {
return this.second;
}
public void setUserMessage(String userMessage) {
this.userMessage = userMessage;
}
public String getUserMessage() {
return this.userMessage;
}
public Delivery() {
month = "JAN";
day = 1;
hour = 12;
minute = 00;
userMessage = "default";
}
@Override
public String toString() {
String data = String.format(month + " %02d, " + "%02d:" + "%02d " + userMessage, day, hour, minute);
return data;
}
public void InputDelivery() {
System.out.println("Enter month of delivery(3 letter abbreviation): ");
setMonth(UserInput.getString(3, 3));
System.out.println("Enter day of delivery: ");
setDay(UserInput.getInt(0, 31));
System.out.println("Enter hour of delivery (0-23): ");
setHour(UserInput.getInt(0, 24));
System.out.println("Enter minute of delivery (0-59): ");
setMinute(UserInput.getInt(0, 59));
System.out.println("Enter message for delivery (40 Character Max): ");
setUserMessage(UserInput.getString(0,40));
System.out.println(toString());
}
}
我真的只需要insertDelivery()方法的帮助,如果你看到其他错误你可以提到它们,但请尽量告诉我这一部分!
答案 0 :(得分:0)
通常,您不能将元素插入数组中。数组是固定长度的(它们包含完全n
个元素,您不能更改数组以包含n+1
或n-1
元素。您可以做的是创建一个具有n+1
元素空间的新数组,并将元素从原始数组复制到新数组中,在创建它时将新元素插入正确的位置。您也可以将其创建为过大而期望获得更多元素(看起来就像您所做的那样),然后开始将元素向上移动。
这听起来像是一个家庭作业问题。如果你有正确的想法,你能问某人(实验室助手,导师,同学)吗?要求“我不允许使用除阵列以外的任何东西”以及插入的需要似乎不一致。
答案 1 :(得分:0)
我首先假设“除数组之外的任何东西”不会扩展到java.util.Arrays
等实用程序类。如果是这样,你需要实现一些我自己使用的方法。
第一步是制作Delivery
工具Comparable<Delivery>
或制作实施Comparator<Delivery>
的新课程。这定义了Delivery
对象的排序。此更改应该很简单,因为您已经定义了compareDelivery
方法。
通过此修改,您可以通过调用Arrays.binarySearch(deliObj,temp)
来获取插入值的索引。如果您不允许使用Arrays
类,this link会显示简单的二进制搜索实现。
1 int[] data; 2 int size; 3 4 public boolean binarySearch(int key) 5 { 6 int low = 0; 7 int high = size - 1; 8 9 while(high >= low) { 10 int middle = (low + high) / 2; 11 if(data[middle] == key) { 12 return true; 13 } 14 if(data[middle] < key) { 15 low = middle + 1; 16 } 17 if(data[middle] > key) { 18 high = middle - 1; 19 } 20 } 21 return false; 22 }
拥有此索引后,您现在需要在此处插入对象。然而,使用数组,这比看起来要困难一些。 Array的长度是不可变的,因此在插入对象之前,必须创建一个比当前数组长一个元素的新Array。你将会在ArrayList
或多或少的幕后做幕后发生的事情。之前已经在StackOverflow中介绍了一个元素插入到数组中,所以我只会引用你a helpful answer。
public static int[] addPos(int[] a, int pos, int num) { int[] result = new int[a.length]; for(int i = 0; i < pos; i++) result[i] = a[i]; result[pos] = num; for(int i = pos + 1; i < a.length; i++) result[i] = a[i - 1]; return result; }