1
我試圖將我的樂器數組複製到一個名爲copy array的新數組中,當我去運行它時,它在我做完while while循環後變得空白,打印出我輸入的音樂家名字。從我可以找到並知道已經我認爲這是將數組複製到另一個的正確方法。謝謝你在先進的用於個人輸入使用java將對象數組複製到另一個數組
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner keyboard= new Scanner(System.in);
final int MAXPEOPLE=100;
instrumentalist [] instrumentalistArr;
instrumentalistArr=new instrumentalist[MAXPEOPLE];
char choice;
int index=0;
int instrumentalistCOUNT=0;
do {
instrumentalist ainstrumentalist = new instrumentalist();
ainstrumentalist.readin();
instrumentalistArr[index]=ainstrumentalist;
instrumentalistCOUNT++;
index++;
System.out.println("would you like to enter another data set yes or no");
choice=keyboard.next().charAt(0);
} while(choice!='n');
instrumentalist [] copyarray;
copyarray=new instrumentalist[instrumentalistArr.length];
for(int i=0; i<instrumentalistCOUNT; i++) {
copyarray[i]=instrumentalistArr[i];
}
for(int i=0; index<instrumentalistCOUNT; i++) {
System.out.println("musician"+" "+ copyarray[index].getmusicianname());
}
}
}
哦哇,我真是愚蠢,完全忘了切換我的變量時,我這麼多讓我copyarray的感謝! – jsch64
@ jsch64您能否請您將答案標記爲已接受? –