多态性:处理对象数组 java
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
}
在这个方法中,
1)我们必须确保两个数组的长度相同。
2)使用浅拷贝
3)如果有String(如果它包含数字),则将其更改为List对象,包含所有可以在String中找到的数字(作为单独的标记)。使用扫描仪扫描字符串
数字。应忽略非数字。
我唯一的疑问是,在 mixedData 数组中,我如何才能找到它是否包含字符串。
望有人解答。
我在您的代码中添加了一些注释来指导您完成解决方案。
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;
}
由于两个数组的长度相同,您必须决定如何处理不包含十进制数的数组元素。例如,interned String 对象 "Nothing here at all"
不包含任何数值,因此一旦我们处理该字符串,它将 return 一个空值。
浅拷贝:由于该字段是原始类型(double
),使用=运算符将其值赋值给右索引处的数组。
您可以轻松更改代码以实现使用扫描仪来根据需要扫描字符串中的数字。创建一个新的扫描器对象并在构造函数中传递您正在处理的字符串。
/**
* 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;
}
}
在这个程序中,不得不用到多态的概念,
我有 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
}
在这个方法中,
1)我们必须确保两个数组的长度相同。
2)使用浅拷贝
3)如果有String(如果它包含数字),则将其更改为List对象,包含所有可以在String中找到的数字(作为单独的标记)。使用扫描仪扫描字符串 数字。应忽略非数字。
我唯一的疑问是,在 mixedData 数组中,我如何才能找到它是否包含字符串。
望有人解答。
我在您的代码中添加了一些注释来指导您完成解决方案。
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;
}
由于两个数组的长度相同,您必须决定如何处理不包含十进制数的数组元素。例如,interned String 对象
"Nothing here at all"
不包含任何数值,因此一旦我们处理该字符串,它将 return 一个空值。浅拷贝:由于该字段是原始类型(
double
),使用=运算符将其值赋值给右索引处的数组。您可以轻松更改代码以实现使用扫描仪来根据需要扫描字符串中的数字。创建一个新的扫描器对象并在构造函数中传递您正在处理的字符串。
/**
* 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;
}
}