java泛型:定义对象
我试图学习Java,但在理解泛型方面遇到了困难。我试图定义一个整型数据类型的对象,然后使用该对象调用一个泛型方法 在API和一些web资源的指导下,我一直在尝试各种方法,但我不知道在简单定义对象方面是否走上了正确的道路 SearchSortAlgorithms.java:java泛型:定义对象,java,generics,Java,Generics,我试图学习Java,但在理解泛型方面遇到了困难。我试图定义一个整型数据类型的对象,然后使用该对象调用一个泛型方法 在API和一些web资源的指导下,我一直在尝试各种方法,但我不知道在简单定义对象方面是否走上了正确的道路 SearchSortAlgorithms.java: public class SearchSortAlgorithms<T> implements SearchSortADT<T> { public void quickSort(T[] list, in
public class SearchSortAlgorithms<T> implements SearchSortADT<T>
{
public void quickSort(T[] list, int length)
{
recQuickSort(list, 0, length - 1);
}
}
公共类SearchSortAlgorithms实现SearchSortADT
{
公共void快速排序(T[]列表,整数长度)
{
重新快速排序(列表,0,长度-1);
}
}
TestQuickSort.java
public class TestQuickSort
{
static void main(String [] args)
{
// define an Integer array of 50000 elements
Integer[] anArray = new Integer[5000];
// load the array with random numbers using
// a for loop and Math.random() method - (int)(Math.random()*50000)
for (int i = 0; i < anArray.length; i++) {
anArray[i] = (int)(Math.random() * i);
}
// define an object of SearchSortAlgorithm with Integer data type
// use this object to call the quickSort method with parameters: your array name and size-50000
Integer aSortedArray = new Integer(5000);
public void quickSort(anArray, 5000) {
TestQuickSort<Integer> aSortedArray = new TestQuickSort<Integer>();
return aSortedArray.quickSort(anArray, 5000);
}
// print out the first 50 array elements with a for loop
// they have to be sorted now
for (int k = 0; k <= 50; k++) {
System.out.print(aSortedArray[k] + " ");
}
}
}
公共类TestQuickSort
{
静态void main(字符串[]参数)
{
//定义50000个元素的整数数组
整数[]anArray=新整数[5000];
//使用随机数加载数组
//for循环和Math.random()方法-(int)(Math.random()*50000)
for(int i=0;i 对于(int k=0;k忽略代码中的任何其他潜在错误(或由于其在此处的显示),您正在尝试在main
中声明另一个方法
public int SearchSortAlgorithm () {
TestQuickSort<Integer> aSortedArray = new TestQuickSort<Integer>();
aSortedArray = quickSort(anArray, 5000);
}
…并像这样调用您的main
方法
int[] aSortedArray = searchSortAlgorithm(anArray);
for (int k = 0; k <= 50; k++) { // would be better to use aSortedArray.length
System.out.print(aSortedArray[k] + " ");
}
int[]aSortedArray=searchSortAlgorithm(anArray);
对于(int k=0;k哪里有错误?我的意思是,哪一行?它们在两个单独的文件中?因为它们都是公开的,应该是公开的。我强烈建议你读一本关于Java的入门书或遵循。照现在的情况,你的代码…真的毫无意义。有很多错误,甚至都无法编译。我刚刚更新了我的代码来反映你的问题我刚刚更新了我的代码@Makoto
public static int[] searchSortAlgorithm (final int[] anArray) {
TestQuickSort<Integer> aSortedArray = new TestQuickSort<Integer>();
return quickSort(anArray, 5000);
}
int[] aSortedArray = searchSortAlgorithm(anArray);
for (int k = 0; k <= 50; k++) { // would be better to use aSortedArray.length
System.out.print(aSortedArray[k] + " ");
}