Код: Выделить всё
static BagInterface createCombineBag(BagInterface aBag1, BagInterface aBag2){
if (aBag1 == null || aBag2 == null) {
return null; // Return null if aBag1 or aBag2 is null
}
BagInterface newBag = new LinkedBag(); // Create a new bag to store the combined data
// Add all elements from aBag1 to the new bag
for (String element : aBag1.toArray()) {
newBag.add(element);
}
// Add all elements from aBag2 to the new bag
for (String element : aBag2.toArray()) {
newBag.add(element);
}
return newBag;
}
Код: Выделить всё
Exception in thread "main" java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [Ljava.lang.String; ([Ljava.lang.Object; and [Ljava.lang.String; are in module java.base of loader 'bootstrap')
Пример: если aBag1 содержит данные: "hello", "Hello", "world"
aBag2 содержит данные: "abc", "hello", "def"
newBag содержит: "hello", "Hello", "world", "abc", "hello", "def"
Подробнее здесь: https://stackoverflow.com/questions/773 ... -one-new-a
Мобильная версия