希望用Java编写代码以获取数组中所有项的详细信息(输入长度不同)
如果您正在寻找一种方法来检索希望用Java编写代码以获取数组中所有项的详细信息(输入长度不同),java,arrays,arraylist,string-formatting,Java,Arrays,Arraylist,String Formatting,如果您正在寻找一种方法来检索ItemTest类的Rocket itemList[]的值,您可以在ItemTest类中为Rocket itemList[]编写一个getter,如下所示 //Class Item public class Item { } String A, B; int C, D; // Constructor public Item(String A, String B, int C, int D) { this.A = A; this.B = B t
ItemTest
类的Rocket itemList[]
的值,您可以在ItemTest
类中为Rocket itemList[]
编写一个getter,如下所示
//Class Item
public class Item {
}
String A, B;
int C, D;
// Constructor
public Item(String A, String B, int C, int D) {
this.A = A;
this.B = B
this.C = C;
this.D = D
public String toFormattedString() {
return String.format("%s %s %d %d", A, B, C, D);
//Class ItemTest
public class ItemTest {
Rocket itemList [];
public ItemTest() {
itemList = new Item[5];
doSampleItem();
itemList[0] = new Item("A”, "ABC DEF", 400, 35);
itemList[1] = new Item("aaaa”, "bbbbbbbbbb", 10, 1000);
itemList[2] = new Item("ccc ddddd”, "eeeee ffff jjjj", 66000, 888);
我必须显示数组的格式化输出。如果你看到的话,它接受任意长度的输入。所以我对如何让它工作感到困惑。我使用了normal For Loop和System.out.print,但这并不是为不同长度的输入字符串格式化输出。我没有在主要问题中添加此要求。不知道如何在我的主查询中编辑此内容:(我是新的
public Rocket getItemList() {
return this.itemList;
}