添加新元素时清空数组
Array is cleared when adding a new element
我已经实现了视频租赁服务。
VideoStore
class 维护 Video
个对象的数组。
当我尝试将新视频添加到清单时,它成功插入了新元素。但是之前添加的所有元素都不再可以访问了。
为什么会这样?
public class Video {
String videoName;
boolean checkout;
int rating;
Video (String name){
this.videoName=name;
}
String getName() {
return videoName;
}
void doCheckout() {
this.checkout=true;
}
void doReturn() {
this.checkout=false;
}
void receiveRating(int rating) {
this.rating=rating;
}
int getRating() {
return rating;
}
boolean getCheckout() {
return checkout;
}
}
public class VideoStore {
Video[] store=null;
int checkVideo(String name) {
int count =0;
if(store==null) {
count =0;
}else
for(Video video:store) {
if(video!=null) {
if(video.getName().equals(name))
count=1;
}}
return count;
}
void addVideo(String name) {
int s;
Video video = new Video(name);
if(store==null)
s=0;
else {
s=store.length;
}
Video[] store2 = new Video[s+1];
store2[s]=video;
store=store2;
}
void doCheckout(String name) {
if(store == null || store.length==0) {
System.out.println("Store is empty");
return;
}else
for(Video video:store) {
if(video!=null) {
if(video.getName().equals(name))
video.doCheckout();
}}
}
void doReturn(String name) {
if(store == null || store.length==0) {
System.out.println("Store is empty");
return;
}else
for(Video video:store) {
if(video!=null) {
if(video.getName().equals(name))
video.doReturn();
}}
}
void receiveRating(String name, int rating) {
if(store == null || store.length==0) {
System.out.println("Store is empty");
return;
} else
for(Video video:store) {
if(video!=null) {
if(video.getName().equals(name))
video.receiveRating(rating);
}}
}
void listInventory() {
if(store == null || store.length==0) {
System.out.println("Store is empty");
return;
}else
for(Video video:store) {
if(video!=null) {
System.out.print("---------------------------------------------------------------------------");
System.out.printf("\n\t%-20s\t|\t%-10s\t|\t%-15s\n", "Video Name","Checkout Status","Rating");
System.out.printf("\n\t%-20s\t|\t%-10s\t|\t%-15s\n", video.getName(), video.getCheckout(),video.getRating());
System.out.println("---------------------------------------------------------------------------");
}
}
}
}
import java.util.Scanner;
public class VideoLauncher {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int input=0;
VideoStore vStore =new VideoStore();
while(true) {
System.out.println("\n1. Add Videos : "+"\n"+"2. Check Out Video : "+"\n"+"3. Return Video : "+
"\n"+"4. Receive Rating : "+"\n"+"5. List Inventory : "+"\n"+"6. Exit : ");
System.out.print("Enter Your Choice (1..6) : ");
if(!sc.hasNextInt()) {
sc.next();
input=0;
}
String name;
input =sc.nextInt();
sc.nextLine();
switch(input) {
case 1:
System.out.print("Enter the name of the video you want to add : ");
name =sc.nextLine();
vStore.addVideo(name);
System.out.println("Video '" +name+"' added successfully.");
break;
case 2:
System.out.print("Enter the name of the video you want to check out : ");
name = sc.nextLine();if(vStore.checkVideo(name)==0) {
System.out.println("Video does not exist.");
}else {
vStore.doCheckout(name);
System.out.println("Video '"+name+"' checked out successfully.");
}
break;
case 3:
System.out.print("Enter the name of the video you want to return : ");
name = sc.nextLine();
if(vStore.checkVideo(name)==0) {
System.out.println("Video does not exist.");
}else {
vStore.doReturn(name);
System.out.println("Video '"+name+"' returned successfully.");
}
break;
case 4:
System.out.print("Enter the name of the video you want to rate : ");
name = sc.nextLine();
if(vStore.checkVideo(name)==0) {
System.out.println("Video does not exist.");
}else {
System.out.print("Enter the rating for this video : ");
int rating = sc.nextInt();
vStore.receiveRating(name, rating);
System.out.println("Rating '"+rating+"' has been mapped to the Video '"+name+"'.");
}
break;
case 5:
vStore.listInventory();
break;
case 6:
sc.close();
System.out.print("Exiting...!! Thanks for using the application.");
return;
default:
System.out.println("Please enter correct choice.");
}}
}
}
if try to insert one more video... the previous element is deleted
from inventory
这是预料之中的,因为在方法 addVideo()
中,您不是在制作副本,而是在创建长度更长的 new
array
然后向其中添加一个新的 Video 对象。那将是 store
.
中的唯一元素
要保留之前添加的所有元素,您必须创建 copy of the existing array
。其中一种方法是使用 Arrays
实用程序 class.
的静态方法 copyOf()
public void addVideo(String name) {
int s;
Video video = new Video(name);
if (store == null)
s = 0;
else {
s = store.length;
}
Video[] store = Arrays.copyOf(store, s + 1);
store[s]=video;
}
看看addVideo
方法:
void addVideo(String name) {
int s;
Video video = new Video(name);
if(store==null)
s=0;
else {
s=store.length;
}
Video[] store2 = new Video[s+1];
// This array only has a single item in it: the new video is at the end of the
// array, and the rest of the array is empty.
// Be sure to preserve the original content.
//
// Copying the entire array every time you add a video is not
// very efficient, though, because it means
// that adding a new video will always be an O(n) operation, so you may want to
// consider an alternative implementation here.
store2[s]=video;
// You replace the entire array here
store=store2;
}
在此方法中,您每次都将原始数组替换为仅包含您刚刚添加的视频的数组。
此外,正如我在代码注释中提到的,数组在这里是一种不太理想的数据结构,因为这意味着添加或签出视频都是 O(n) 操作。您可能需要考虑使用散列 table。
我已经实现了视频租赁服务。
VideoStore
class 维护 Video
个对象的数组。
当我尝试将新视频添加到清单时,它成功插入了新元素。但是之前添加的所有元素都不再可以访问了。
为什么会这样?
public class Video {
String videoName;
boolean checkout;
int rating;
Video (String name){
this.videoName=name;
}
String getName() {
return videoName;
}
void doCheckout() {
this.checkout=true;
}
void doReturn() {
this.checkout=false;
}
void receiveRating(int rating) {
this.rating=rating;
}
int getRating() {
return rating;
}
boolean getCheckout() {
return checkout;
}
}
public class VideoStore {
Video[] store=null;
int checkVideo(String name) {
int count =0;
if(store==null) {
count =0;
}else
for(Video video:store) {
if(video!=null) {
if(video.getName().equals(name))
count=1;
}}
return count;
}
void addVideo(String name) {
int s;
Video video = new Video(name);
if(store==null)
s=0;
else {
s=store.length;
}
Video[] store2 = new Video[s+1];
store2[s]=video;
store=store2;
}
void doCheckout(String name) {
if(store == null || store.length==0) {
System.out.println("Store is empty");
return;
}else
for(Video video:store) {
if(video!=null) {
if(video.getName().equals(name))
video.doCheckout();
}}
}
void doReturn(String name) {
if(store == null || store.length==0) {
System.out.println("Store is empty");
return;
}else
for(Video video:store) {
if(video!=null) {
if(video.getName().equals(name))
video.doReturn();
}}
}
void receiveRating(String name, int rating) {
if(store == null || store.length==0) {
System.out.println("Store is empty");
return;
} else
for(Video video:store) {
if(video!=null) {
if(video.getName().equals(name))
video.receiveRating(rating);
}}
}
void listInventory() {
if(store == null || store.length==0) {
System.out.println("Store is empty");
return;
}else
for(Video video:store) {
if(video!=null) {
System.out.print("---------------------------------------------------------------------------");
System.out.printf("\n\t%-20s\t|\t%-10s\t|\t%-15s\n", "Video Name","Checkout Status","Rating");
System.out.printf("\n\t%-20s\t|\t%-10s\t|\t%-15s\n", video.getName(), video.getCheckout(),video.getRating());
System.out.println("---------------------------------------------------------------------------");
}
}
}
}
import java.util.Scanner;
public class VideoLauncher {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int input=0;
VideoStore vStore =new VideoStore();
while(true) {
System.out.println("\n1. Add Videos : "+"\n"+"2. Check Out Video : "+"\n"+"3. Return Video : "+
"\n"+"4. Receive Rating : "+"\n"+"5. List Inventory : "+"\n"+"6. Exit : ");
System.out.print("Enter Your Choice (1..6) : ");
if(!sc.hasNextInt()) {
sc.next();
input=0;
}
String name;
input =sc.nextInt();
sc.nextLine();
switch(input) {
case 1:
System.out.print("Enter the name of the video you want to add : ");
name =sc.nextLine();
vStore.addVideo(name);
System.out.println("Video '" +name+"' added successfully.");
break;
case 2:
System.out.print("Enter the name of the video you want to check out : ");
name = sc.nextLine();if(vStore.checkVideo(name)==0) {
System.out.println("Video does not exist.");
}else {
vStore.doCheckout(name);
System.out.println("Video '"+name+"' checked out successfully.");
}
break;
case 3:
System.out.print("Enter the name of the video you want to return : ");
name = sc.nextLine();
if(vStore.checkVideo(name)==0) {
System.out.println("Video does not exist.");
}else {
vStore.doReturn(name);
System.out.println("Video '"+name+"' returned successfully.");
}
break;
case 4:
System.out.print("Enter the name of the video you want to rate : ");
name = sc.nextLine();
if(vStore.checkVideo(name)==0) {
System.out.println("Video does not exist.");
}else {
System.out.print("Enter the rating for this video : ");
int rating = sc.nextInt();
vStore.receiveRating(name, rating);
System.out.println("Rating '"+rating+"' has been mapped to the Video '"+name+"'.");
}
break;
case 5:
vStore.listInventory();
break;
case 6:
sc.close();
System.out.print("Exiting...!! Thanks for using the application.");
return;
default:
System.out.println("Please enter correct choice.");
}}
}
}
if try to insert one more video... the previous element is deleted from inventory
这是预料之中的,因为在方法 addVideo()
中,您不是在制作副本,而是在创建长度更长的 new
array
然后向其中添加一个新的 Video 对象。那将是 store
.
要保留之前添加的所有元素,您必须创建 copy of the existing array
。其中一种方法是使用 Arrays
实用程序 class.
copyOf()
public void addVideo(String name) {
int s;
Video video = new Video(name);
if (store == null)
s = 0;
else {
s = store.length;
}
Video[] store = Arrays.copyOf(store, s + 1);
store[s]=video;
}
看看addVideo
方法:
void addVideo(String name) {
int s;
Video video = new Video(name);
if(store==null)
s=0;
else {
s=store.length;
}
Video[] store2 = new Video[s+1];
// This array only has a single item in it: the new video is at the end of the
// array, and the rest of the array is empty.
// Be sure to preserve the original content.
//
// Copying the entire array every time you add a video is not
// very efficient, though, because it means
// that adding a new video will always be an O(n) operation, so you may want to
// consider an alternative implementation here.
store2[s]=video;
// You replace the entire array here
store=store2;
}
在此方法中,您每次都将原始数组替换为仅包含您刚刚添加的视频的数组。
此外,正如我在代码注释中提到的,数组在这里是一种不太理想的数据结构,因为这意味着添加或签出视频都是 O(n) 操作。您可能需要考虑使用散列 table。