Java嵌套for循环重复错误
我创建了这个包,问题是当它运行时,它会在存储箱之间重复相同的目录条目。任何帮助都将不胜感激! 问题可能在详细的库存方法。 它应该为bin B创建一个新的随机bin项(类型、标题、艺术家),而不是像现在这样使用bin a中的相同元素。除此之外,输出是正确的。正确生成并显示SKU编号 它应该做什么:详细库存方法应该使用for each循环遍历仓库中的所有仓位。对于每个bin,应使用字符串“bin”扩展s,后跟bin的名称,后跟冒号,\n以开始新行。然后,它应该在当前bin中的所有bin项之间循环,每个bin项扩展一行新的文本,该行以在输入目录中查找当前bin项的SKU结果开始,并以逗号后跟bin项的字符串表示形式继续 其他信息: *当i的值与目录相对应时,它们是对象。它们实际上是不同类型的物理媒体,如DVD或磁带,具有标题、艺术家和SKU属性Java嵌套for循环重复错误,java,arraylist,nested-loops,Java,Arraylist,Nested Loops,我创建了这个包,问题是当它运行时,它会在存储箱之间重复相同的目录条目。任何帮助都将不胜感激! 问题可能在详细的库存方法。 它应该为bin B创建一个新的随机bin项(类型、标题、艺术家),而不是像现在这样使用bin a中的相同元素。除此之外,输出是正确的。正确生成并显示SKU编号 它应该做什么:详细库存方法应该使用for each循环遍历仓库中的所有仓位。对于每个bin,应使用字符串“bin”扩展s,后跟bin的名称,后跟冒号,\n以开始新行。然后,它应该在当前bin中的所有bin项之间循环,每
- 虽然这似乎没有意义,但数据应该随机生成。(这个类是说明继承的更大概念/课程的一部分。)
- 我将共享同一SKU的bin中所有项目的集合称为“bin项目”
- Bin类的每个对象(以完整代码显示)表示虚拟仓库中的一个Bin。Bin对象有两个实例变量:一个字符串包含Bin名称,另一个ArrayList包含Bin中存储的每个SKU的BinItem。Bin类的toString方法使用BinItem类的toString方法。它生成只涉及SKU和数量的列表
Bin B: DVD - Greatest Hits Volume 2 (Limp Bizkit), SKU 1234-3: 1000
我还有一个相同的卡带类,它也扩展了MusicMedia。还有另外两个磁盘子类,称为CompactDisk和DigitalVideoDisk。这两个类也几乎完全相同,所以我在下面粘贴了DVD类
public class DigitalVideoDisk extends Disk
{
/**
* Constructor for objects of class DigitalVideoDisk
*/
public DigitalVideoDisk( String title, String artist, String sku )
{
super(title, artist, sku);
}
public String getMediaType()
{
return "DVD";
}
}
public class BinItem
{
private String mySKU;
private int myQuantity;
public BinItem( String sku, int quantity )
{
mySKU = sku;
myQuantity = quantity;
}
public String getSKU()
{
return mySKU;
}
public int getQuantity()
{
return myQuantity;
}
public String toString()
{
return "SKU " + getSKU() + ": " + getQuantity();
}
}
public class Bin
{
private String myName; //bin name
private ArrayList<BinItem> myContents; //contains a binItem
public Bin( String name )
{
myName = name;
myContents = new ArrayList<BinItem>();
}
public String getName()
{
return myName;
}
public ArrayList<BinItem> getContents()
{
return myContents;
}
public void add( BinItem b )
{
myContents.add( b );
}
public String toString()
{
String s = "Bin " + myName + ":\n";
for ( BinItem b : myContents )
s += b + "\n";
return s;
}
}
公共类数字视频磁盘扩展磁盘
{
/**
*DigitalVideoDisk类对象的构造函数
*/
公共数字视频磁盘(字符串标题、字符串艺术家、字符串sku)
{
超级(标题、艺术家、sku);
}
公共字符串getMediaType()
{
返回“DVD”;
}
}
公共类项目
{
私有字符串mySKU;
私人数量;
公共BinItem(字符串sku,整数数量)
{
mySKU=sku;
我的数量=数量;
}
公共字符串getSKU()
{
返回mySKU;
}
公共整数getQuantity()
{
返回我的数量;
}
公共字符串toString()
{
返回“SKU”+getSKU()+”:“+getQuantity();
}
}
公共类垃圾箱
{
私有字符串myName;//bin name
private ArrayList myContents;//包含一个binItem
公共Bin(字符串名称)
{
我的名字=名字;
myContents=newarraylist();
}
公共字符串getName()
{
返回我的名字;
}
公共ArrayList getContents()
{
返回mycontent;
}
公共作废添加(b项)
{
增加(b);
}
公共字符串toString()
{
字符串s=“Bin”+myName+:\n”;
用于(b项:霉菌含量)
s+=b+“\n”;
返回s;
}
}
这是一个很长的问题。有没有可能把它缩小到重现你的问题所必需的程度?@LutzHorn我可以试试……我想让它尽可能清楚。可能重复的@Mauren更新了这个问题,包括新的编译器错误
public static MusicMedia getMediaBySKU(ArrayList<MusicMedia> catalog, String sku)
{
for ( MusicMedia m : catalog ) {
if ( m.getSKU().equals(sku) )
return m;
}
return null;
}
for ( Bin bn : warehouse ){
s += "Bin " + bn.getName() + ":\n";
for ( BinItem item : bn.getContents() ){
MusicMedia mm = getMediaBySKU(catalog, item.getSKU());
s += mm + ", " + item + "\n";
}
}
public class testMusicMedia
{
public static ArrayList<MusicMedia> MakeMusicCatalog( int size )
{
String[] titles =
{
"Greatest Hits Volume 1", "Greatest Hits Volume 2",
"The Best Of", "Love Songs",
"The Early Years", "The Later Years"
};
String[] artists =
{
"Michael Jackson", "Eminem",
"The Beatles", "Shania Twain",
"Limp Bizkit"
};
ArrayList<MusicMedia> catalog = new ArrayList<MusicMedia>();
Random rn = new Random();
for ( int i = 0 ; i < size ; i++ )
{
MusicMedia m;
int mediatype = rn.nextInt( 3 );
String title = titles[ rn.nextInt( titles.length ) ];
String artist = artists[ rn.nextInt( artists.length ) ];
String sku = "1234-" + i;
if ( mediatype == 0 )
m = new CompactDisk( title, artist, sku );
else if ( mediatype == 1 )
m = new DigitalVideoDisk( title, artist, sku );
else
m = new CassetteTape( title, artist, sku );
catalog.add( m );
}
return catalog;
}
public static String lookupMedia( ArrayList<MusicMedia> catalog,
String sku )
{
for ( MusicMedia m : catalog )
{
if ( m.getSKU().equals( sku ))
return "SKU is in catalog";
}
return "SKU not in catalog";
}
public static String detailedInventory( ArrayList<MusicMedia> catalog, ArrayList<Bin> warehouse )
{
String s = "";
for ( Bin bn : warehouse ){
s += "Bin " + bn.getName() + ":\n";
for (int i = 0; i < bn.getContents().size(); i++){
s += catalog.get(i) + ", " + bn.getContents().get(i) + "\n";
}
}
s += "\n";
return s;
}
public static void main( String[] args )
{
ArrayList<MusicMedia> catalog = MakeMusicCatalog( 10 );
ArrayList<Bin> warehouse = new ArrayList<Bin>();
Bin a = new Bin( "A" );
Bin b = new Bin( "B" );
warehouse.add( a );
warehouse.add( b );
a.add( new BinItem( "1234-0", 500 ) );
a.add( new BinItem( "1234-1", 25 ) );
a.add( new BinItem( "1234-2", 7720 ) );
b.add( new BinItem( "1234-3", 1000 ) );
System.out.println( detailedInventory( catalog, warehouse ) );
}
}
public class MusicMedia
{
private String myTitle,
myArtist,
mySKU;
public MusicMedia( String title, String artist, String sku )
{
myTitle = title;
myArtist = artist;
mySKU = sku;
}
public String getTitle()
{
return myTitle;
}
public String getArtist()
{
return myArtist;
}
public String getMediaType()
{
return "Unknown";
}
public String getSKU()
{
return mySKU;
}
public String toString()
{
return " - " + getTitle() + " (" + getArtist() + ")";
}
}
public class Disk extends MusicMedia
{
/**
* Constructor for objects of class Disk
*/
public Disk( String title, String artist, String sku )
{
super(title, artist, sku);
}
public String getMediaType()
{
return "Disk";
}
public String toString()
{
return getMediaType() + super.toString();
}
}
public class DigitalVideoDisk extends Disk
{
/**
* Constructor for objects of class DigitalVideoDisk
*/
public DigitalVideoDisk( String title, String artist, String sku )
{
super(title, artist, sku);
}
public String getMediaType()
{
return "DVD";
}
}
public class BinItem
{
private String mySKU;
private int myQuantity;
public BinItem( String sku, int quantity )
{
mySKU = sku;
myQuantity = quantity;
}
public String getSKU()
{
return mySKU;
}
public int getQuantity()
{
return myQuantity;
}
public String toString()
{
return "SKU " + getSKU() + ": " + getQuantity();
}
}
public class Bin
{
private String myName; //bin name
private ArrayList<BinItem> myContents; //contains a binItem
public Bin( String name )
{
myName = name;
myContents = new ArrayList<BinItem>();
}
public String getName()
{
return myName;
}
public ArrayList<BinItem> getContents()
{
return myContents;
}
public void add( BinItem b )
{
myContents.add( b );
}
public String toString()
{
String s = "Bin " + myName + ":\n";
for ( BinItem b : myContents )
s += b + "\n";
return s;
}
}