Java 如何在数组中调用存储的数据(字符串)?
我想把标题打印出来Java 如何在数组中调用存储的数据(字符串)?,java,arrays,jgrasp,Java,Arrays,Jgrasp,我想把标题打印出来 import java.text.SimpleDateFormat; public class TheMixtape { private String Title; private String Artist; private String Genre; private int Minutes; private int Seconds; private int Duration; public TheMixtape() { String Title[]=
import java.text.SimpleDateFormat;
public class TheMixtape
{
private String Title;
private String Artist;
private String Genre;
private int Minutes;
private int Seconds;
private int Duration;
public TheMixtape()
{
String Title[]={"BROCCOLI", "Ready Set Go(VLone)", "Chill Bill", "Rara", "Too Much Sauce", "Heathens", "No Heart", "Water", "Wake Up"};
String Artist[]={"Big Baby D.R.A.M.(Prod. By. J Gramm)", "Lil Uzi Vert", "Rob Stone", "Travis Scott ft(Lil Uzi Vert)", "Future","21 Pilots", "21 Savage", "Ugly God", "Fetty Wap"};
String Genre[]={"Hip-Hop/Rap", "Trap Anthem", "Smooth Jazz", "Pop Music", "Trap Anthem", "Pop Song", "Rap", "Trap/Rap/Pop", "Pop"};
//int D[]={ 3:45, 3:42, 2:57, 3:07, 3:38, 3:11, 3:55, 2:19, 3:44}; ignore this
}
public String getTitle()
{
return Title;
}
public static void main(String[]args)
{
TheMixtape Info = new TheMixtape();
System.out.println(" Title "+"Artist "+"Genre "+"Duration");
for (int k=0;k<10;k++)
{
System.out.print(Info.getTitle());
}
}
}
然后它会给我数组中的第一个字符串,名为brocoli。
如何将getTitle()更改为
注意
数组应该是类的一部分。您在构造函数中声明了它们,此时它们超出了范围,无法使用
public class TheMixtape{
String Title[]={"BROCCOLI", "Ready Set Go(VLone)", "Chill Bill", "Rara", ...};
String Artist[]={"Big Baby D.R.A.M.(Prod. By. J Gramm)", "Lil Uzi Vert", ...};
String Genre[]={"Hip-Hop/Rap", "Trap Anthem", "Smooth Jazz", "Pop Music"...};
public TheMixtape()
{
}
此外,Java编程标准通常使用与方法相同的大写规则,例如:
String title[]={"BROCCOLI", "Ready Set Go(VLone)", ...};
将getTitle()更改为
注意
数组应该是类的一部分。您在构造函数中声明了它们,此时它们超出了范围,无法使用
public class TheMixtape{
String Title[]={"BROCCOLI", "Ready Set Go(VLone)", "Chill Bill", "Rara", ...};
String Artist[]={"Big Baby D.R.A.M.(Prod. By. J Gramm)", "Lil Uzi Vert", ...};
String Genre[]={"Hip-Hop/Rap", "Trap Anthem", "Smooth Jazz", "Pop Music"...};
public TheMixtape()
{
}
此外,Java编程标准通常使用与方法相同的大写规则,例如:
String title[]={"BROCCOLI", "Ready Set Go(VLone)", ...};
使用数组索引,例如
System.out.println(Info.Title[0])
0]索引数组中的第一个元素brocoli。使用数组索引,例如System.out.println(Info.Title[0])
0为数组中的第一个元素Brocoli编制索引。还需要修复构造函数。当我将代码粘贴到数组中时,它在Mixstape中出现错误。java:22:error:array required,但字符串找到了返回标题[index-1];//如果通过1,则获得第0个索引也需要修复构造函数。当我将代码粘贴到其中时,它给出了一个错误TheMixtape.java:22:error:array required,但字符串找到返回标题[index-1];//如果通过1,则得到第0个索引