在学校里面碰到个数据结构的试验
要求输入一个数组 经过处理以后返回一个数组
但是老师要求对所有的数据类型都能处理
我在一个类里面定义了一个方法
public Object[] xxxx(Object[] inArray){ ………… }
调用的时候使用了(Object[])a强制的类型转换
编译提示不可转换的类型Java 里面有没有像VB 的Variant 这样的未定义数据类型
或者这个还有其他的解决方案吗?
要求输入一个数组 经过处理以后返回一个数组
但是老师要求对所有的数据类型都能处理
我在一个类里面定义了一个方法
public Object[] xxxx(Object[] inArray){ ………… }
调用的时候使用了(Object[])a强制的类型转换
编译提示不可转换的类型Java 里面有没有像VB 的Variant 这样的未定义数据类型
或者这个还有其他的解决方案吗?
我测试的时候输入了一个int[] 类型的数组
编译提示上述错误
{
public Object[] delRepetition(Object[] inArray)
{ int length=inArray.length;
Object[] b = new Object[length];
int index = -1;
int lengthNew=length;
boolean reget;
for (int i=0;i<length ;i++ )
{ reget = false;
for (int j=0;j<=index ;j++ )
{ if (inArray[i]==b[j])
{ reget = true;
lengthNew--;
continue;
}
}
if (!reget)
{ index++;
b[index]=inArray[i];}
}
for (int i=index+1;i<length ;i++ )
{b[i]=null;}
return b;
}
}这个是测试类
class OrderListTest
{
public static void main(String[] args)
{
int[] testArray = {1,2,31,1,1,1,1,6,6,132,1};
int length=testArray.length;
for (int i=0;i<=length-1 ;i++ )
{
System.out.print(testArray[i]+" ");
}
System.out.println(); OrderList a = new OrderList();
testArray=(int[])a.delRepetition((Object[])testArray);
for (int i=0;i<=testArray.length-1 ;i++ )
{
System.out.print(testArray[i]+" ");
}
}
}
{
public static void main(String[] args)
{
int[] testArray = {1,2,31,1,1,1,1,6,6,132,1};
int length=testArray.length;
Integer numArr = new Integer[length]
for (int i=0;i<=length-1 ;i++ )
{
numArr[i] = new Integer(testArray[i])
System.out.print(testArray[i]+" ");
System.out.print(numArr[i]+" ");
}
System.out.println(); OrderList a = new OrderList();
Object[] testArray=a.delRepetition(numArr);
for (int i=0;i<=testArray.length-1 ;i++ )
{
System.out.print(testArray[i]+" ");
}
}
}
不知这样可以吗
class OrderListTest
{
public static void main(String[] args)
{
int[] testArray = {1,2,31,1,1,1,1,6,6,132,1};
int length=testArray.length;
Integer[] numArr = new Integer[length]
for (int i=0;i<=length-1 ;i++ )
{
numArr[i] = new Integer(testArray[i])
System.out.print(testArray[i]+" ");
System.out.print(numArr[i]+" ");
}
System.out.println(); OrderList a = new OrderList();
Object[] testArray=a.delRepetition(numArr);
for (int i=0;i<=testArray.length-1 ;i++ )
{
System.out.print(testArray[i]+" ");
}
}
}
不知这样可以吗
int[] testArray = {1,2,31,1,1,1,1,6,6,132,1};
期望的输出是 1 2 31 6 132你的那个代码试过了
这个地方好像有问题
Object[] testArray=a.delRepetition(numArr);
改成了
Object[] testaArray=a.delRepetition(numArr);
最后输出的也是 System.out.print(testaArray[i]+" ");
得到结果1 2 31 1 1 1 1 6 6 132 1
Integer[] testArray 下面调用
testArray=(Integer[])a.delRepetition(testArray);
能编译
但是运行的时候提示
java.lang.ClassCastException 异常
[Ljava.lang.Object;can not be cast to [Ljava.langInteger;
也是提示ClassCastException
{
Collection collection = new HashSet();
int length=inArray.length;
for (int i=0;i<length ;i++ )
{
collection.add(inArray[i]);
}
Object[] b = collection.toArray();
return b;
}
我改了你的这个方法,不知这样可以吗
Object[] testaArray=a.delRepetition(numArr);
这样运行会报错
当然报错,此时的testArray已经是Object[]类型的了
import java.util.Collection;
import java.util.HashSet;class OrderListTest
{
public static void main(String[] args)
{
int[] testArray = {1,2,31,1,1,1,1,6,6,132,1};
int length=testArray.length;
Integer[] numArr = new Integer[length];
for (int i=0;i<=length-1 ;i++ )
{
numArr[i] = new Integer(testArray[i]);
System.out.print(testArray[i]+" ");
System.out.print(numArr[i]+" ");
}
System.out.println(); OrderList a = new OrderList();
Object[] testArray1=a.delRepetition(numArr);
for (int i=0;i< testArray1.length ;i++ )
{
System.out.print(testArray1[i]+" ");
}
}
}
class OrderList
{
Object[] delRepetition(Object[] inArray)
{
Collection collection = new HashSet();
int length=inArray.length;
for (int i=0;i<length ;i++ )
{
collection.add(inArray[i]);
}
Object[] b = collection.toArray();
return b;
}
}
把OrderListTest后面一段
改成了
Object[] b=a.delRepetition(testArray);
Integer c[]=new Integer[b.length];
for (int i=0;i<=b.length-1 ;i++ )
{
c[i]=(Integer)b[i];
System.out.print(c[i]+" "); }谢谢哦