英文:
Polymorphism: handling an array of objects java
问题
在这个程序中,我必须使用多态的概念。
我有1个名为Data的抽象超类,以及2个名为List和Single的子类。Single接受一个双精度值(构造函数:public Single(value))。List接受一个双精度数组(构造函数:List(double[] arr)),在我的主方法中,以下数组...
public static void main(String[] args) {
Object[] mixedData = {
new Single(2.4),
"The data is 3.6",
new List(new double[] {3.2,6.8}),
"Nothing here at all",
new List(new double[] {1.2,7.9,4.5}),
"Anda 1 anda 2 anda 3",
new Single(9.8)
};
我必须使用以下方法将这个Object[]数组转换为Data[]数组:
public static Data[] convert(Object[] objects){
final int MAX_LIST_SIZE = 10;
//***** YOUR CODE HERE *****
objects= new Object[MAX_LIST_SIZE];
Data[] data= new Data[MAX_LIST_SIZE];
data = (Data[]) objects;
for(int i=0; i<data.length; i++) {
}
return null; //Dummy statement - replace it
}
在这个方法中,
-
我们必须确保两个数组具有相同的长度。
-
使用浅复制
-
如果有一个字符串(如果它包含一个数字),则将其更改为List对象,其中包含字符串中可以找到的所有数字(作为单独的标记)。使用Scanner扫描字符串以查找数字。非数字应该被忽略。
我的唯一疑问是,在mixedData数组中,如何判断是否包含字符串。
希望有人会回答。
英文:
In this program, I have to use the concept of polymorphism,
I have 1 abstract superclass named Data, and 2 subclasses named List and Single. Single accepts a double value(Constructor: public Single(value)). List accepts an array of doubles.( Constructor: List(double[] arr)), and in my main method, the following array,...
public static void main(String[] args) {
Object[] mixedData = {
new Single(2.4),
"The data is 3.6",
new List(new double[] {3.2,6.8}),
"Nothing here at all",
new List(new double[] {1.2,7.9,4.5}),
"Anda 1 anda 2 anda 3",
new Single(9.8) };
I have to convert this Object[] array into Data[] array using a method:
public static Data[] convert(Object[] objects){
final int MAX_LIST_SIZE = 10;
//***** YOUR CODE HERE *****
objects= new Object[MAX_LIST_SIZE];
Data[] data= new Data[MAX_LIST_SIZE];
data = (Data[]) objects;
for(int i=0; i<data.length; i++) {
}
return null; //Dummy statement - replace it
}
In this method,
1)we have to make sure that both arrays are of same length.
2)Use shallow copy
3)If there is a String(if it contains a number), then change it to List object, containing all the numbers that can be found(as separate tokens) in the String. Use a Scanner to scan the String for
numbers. Non-numbers should be ignored.
My Only doubt is that, in mixedData array, how can I find if it contains a String.
hope someone will answer.
答案1
得分: 1
以下是您代码中的注释部分的中文翻译:
public static Data[] convert(Object[] objects){
// 如果对象数组包含超过10个元素,应该怎么处理?
// final int MAX_LIST_SIZE = 10;
// 在这里你清空了输入对象的内容,为什么?
// objects= new Object[MAX_LIST_SIZE];
// 将数据数组的长度设置为输入对象数组的长度
Data[] data= new Data[objects.length];
// 这是不能这样做的
// data = (Data[]) objects;
for(int i=0; i<objects.length; i++) {
if(objects[i] instanceof Single) {
data[i] = (Single) objects[i];
}else if(objects[i] instanceof List) {
data[i] = (List) objects[i];
}else if(objects[i] instanceof String) {
String string = (String) objects[i];
// 使用Scanner查找所有的double值
// 将这些double值添加到一个列表中
// 将该列表添加到data[i]
}
}
return data;
}
英文:
I added some comments to your code to guide you through the solution.
public static Data[] convert(Object[] objects){
// If the objects array contains more than 10 elements what to do?
// final int MAX_LIST_SIZE = 10;
// Here you clear the content of the input objects, why?
//objects= new Object[MAX_LIST_SIZE];
// Set the length of data to the length of the input object array
Data[] data= new Data[objects.length];
// This cannot be done
// data = (Data[]) objects;
for(int i=0; i<objects.length; i++) {
if(objects[i] instanceof Single) {
data[i] = (Single) objects[i];
}else if(objects[i] instanceof List) {
data[i] = (List) objects[i];
}else if(objects[i] instanceof String) {
String string = (String) objects[i];
// Find all doubles with Scanner
// Add the doubles to a List
// Add the List to data[i]
}
}
return data;
}
答案2
得分: 1
-
由于这两个数组的长度相同,您需要决定如何处理不包含小数的数组元素。例如,内联的字符串对象
"Nothing here at all"
不包含任何数字值,因此一旦我们处理字符串,它将返回一个空值。 -
浅复制:由于该字段是原始类型(
double
),请使用等号运算符将其值分配给右侧索引处的数组。 -
您可以轻松更改代码以实现使用 Scanner 扫描字符串以查找所需的数字。创建一个新的 Scanner 对象,并将要处理的字符串传递给构造函数。
英文:
-
Since both arrays are of same length, you have to decide how to handle an array element that doesn't contain a decimal number. For example, the interned String object
"Nothing here at all"
doesn't contain any numerical values, so once we process the string, it will return a null value. -
Shallow copy: since the field is a primitive type (
double
), use the = operator to assign its value to the array at the right index. -
You can change the code easily to implement the use of a Scanner to scan the String for numbers as required. Create a new scanner object and pass the string you're processing in the constructor.
/**
* Output:
* Single obj: 2.4
* Single obj: 3.6
* List obj: 3.2 6.8
* Data obj: null
* List obj: 1.2 7.9 4.5
* List obj: 1.0 2.0 3.0
* Single obj: 9.8
*
* @author martinfall
*/
public class TestData {
public static void main(String[] args) {
// Given
Object[] mixedData = {
new Single(2.4),
"The data is 3.6",
new List(new double[]{3.2, 6.8}),
"Nothing here at all",
new List(new double[]{1.2, 7.9, 4.5}),
"Anda 1 anda 2 anda 3",
new Single(9.8)};
// Convert mixedData and assign the result to a Data array
Data[] arr = convert(mixedData);
// Print to console (Not required but helpful to see the output of each obj)
for (Data datum : arr) {
if (datum instanceof Single) {
System.out.print("Single obj: ");
System.out.println(((Single) datum).value); // Can encapsulate
} else if (datum instanceof List) {
System.out.print("List obj: ");
for (double num : ((List) datum).arr) {
System.out.print(num + " ");
}
System.out.println();
} else {
// Since required that both arrays be equal size,
// not sure how to handle an element of mixedData that doesn't
// contain any decimal numbers
System.out.println("Data obj: " + datum);
}
}
}
public static Data[] convert(Object[] objects) {
// Find the length of objects and assign it to MAX_LIST_SIZE
final int MAX_LIST_SIZE = objects.length;
// Create a new array of Data objects using the length of objects
Data[] arr = new Data[MAX_LIST_SIZE];
// Loop throught the array and copy each element as required
for (int i = 0; i < MAX_LIST_SIZE; i++) {
if (objects[i] instanceof Single) {
arr[i] = (Data) objects[i]; // Shallow copy
} else if (objects[i] instanceof List) {
arr[i] = (Data) objects[i];
} else if (objects[i] instanceof String) {
// Since both arrays have to be the same length, we have to add
// the null value that is returned if a string doesn't contain
// a numerical value
arr[i] = processString((String) objects[i]);
}
}
return arr;
}
public static Data processString(String str) {
// Regular expression to match double values
String regex = "^[-+]?\\d*(\\.\\d+)?$";
// Counter variable to use to find out if list or single is returned
int count = 0;
// Create a blank Data variable
Data d = null;
// Split the String
String[] split = str.split(" ");
// Determine if Single or List
for (String s : split) {
if (s.matches(regex)) {
count++;
}
}
// If count is 1, return a Single
if (count == 1) {
for (String s : split) {
if (s.matches(regex)) {
d = new Single(Double.parseDouble(s));
}
}
} else if (count > 1) {
// Create a new array as large as count
double[] arr = new double[count];
// Index of arr
int arrIndex = 0;
for (String s : split) {
if (s.matches(regex)) {
arr[arrIndex] = Double.parseDouble(s);
arrIndex++;
}
}
d = new List(arr);
}
return (Data) d;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论