![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jul 2005
Posts: 15
Rep Power: 0
![]() |
Problem in my Java Code
I am trying to write a code using recursive method to check if a sorted list contains a given index. However, I receive compile errors when trying to compile the code.
Errors Received: 1. asList(java.lang.Object[]) in java.util.Arrays cannot be applied to (int[]) 2. non-static variable cannot be referenced from a static context. 3. cannot resolve symbol method contains(java.util.List,int) What is wrong?... import java.io.*;
import java.util.*;
public class NumberTest
{
private int values[] = {2,4,7,9,10,14,16,17,20,26,28,32,40};
private List aList = new ArrayList(Arrays.asList(values);
public static boolean contains(List aList, Integer anInteger)
{
if (aList.size() == 0) return false;
return contains(aList, anInteger, 0, aList.size()-1);
}
public static boolean contains(List aList, Integer anInteger, int beginIndex, int endIndex)
{
if (beginIndex == endIndex)
return aList.get(beginIndex).equals(anInteger);
if (anInteger.intValue()<((Integer)aList.get(beginIndex)).intValue())
return false;
if (anInteger.intValue()>((Integer)aList.get(beginIndex)).intValue())
return false;
int middleIndex = (endIndex + beginIndex)/2;
Integer middleInteger = (Integer)aList.get(middleIndex);
if (anInteger.intValue() < middleInteger.intValue())
return contains(aList,anInteger,beginIndex,middleIndex);
return contains(aList,anInteger,middleIndex+1, endIndex);
}
public static void main(String args[])
{
System.out.print(contains(aList,16));
}
} |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
I'm new to java but in one of the tutorials i'm studying, it says that ArrayList has to have Objects as parameters and not primitive types. You can use type wrappers (Integer instead of int) but i don't know how to manipulate'em yet. Here's the definition for an Integer array of object:
private Integer[] values = new Integer[13];
//now you need to get these values into the new array:{2,4,7,9,10,14,16,17,20,26,28,32,40};
// I'm not sure how to do this |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
.. the wrapper's methods simply call the wrapped class's methods (seems kinda pointless though
)
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
|
|
#4 | |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Jul 2005
Posts: 15
Rep Power: 0
![]() |
I have changed the code but I still get an error message when I compile it
import java.io.*;
import java.util.*;
public class NumberTest
{
private static int[] values = {2,4,7,9,10,14,16,17,20,26,28,32,40};
private static List aList;
public static boolean contains(List aList, Integer anInteger)
{
initList();
if (aList.size() == 0) return false;
return contains(aList, anInteger, 0, aList.size()-1);
}
public static boolean contains(List aList, Integer anInteger, int beginIndex, int endIndex)
{
initList();
if (beginIndex == endIndex)
return aList.get(beginIndex).equals(anInteger);
if (anInteger.intValue()<((Integer)aList.get(beginIndex)).intValue())
return false;
if (anInteger.intValue()>((Integer)aList.get(beginIndex)).intValue())
return false;
int middleIndex = (endIndex + beginIndex)/2;
Integer middleInteger = (Integer)aList.get(middleIndex);
if (anInteger.intValue() < middleInteger.intValue())
return contains(aList,anInteger,beginIndex,middleIndex);
return contains(aList,anInteger,middleIndex+1, endIndex);
}
private static void initList()
{
if (aList != null)
return;
Integer[] valuesObj = new Integer[values.length];
for (int i = 0; i < values.length; i++)
valuesObj[i] = new Integer(values[i]);
aList = new ArrayList(Arrays.asList(valuesObj));
}
public static void main(String[] args)
{
System.out.print(contains(aList,16)); // Cannot resolve symbol method (java.util.List, int).
}
} |
|
|
|
|
|
#6 | |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
Quote:
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
|
|
|
#7 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
I agree with stevengs, you need to be as verbose as the linux kernel so we can help you better.
Since I am in the process of learning java, I though it would be a good practice to get your code to compile, here it is: //package numbertest;
import java.io.*;
import java.util.*;
public class NumberTest
{
//notice the static keywords before the declarations are necessary here
private static Integer[] values = {new Integer(2),new Integer(4), new Integer(7),
new Integer(9),new Integer(10),new Integer(14),new Integer(16),
new Integer(17),new Integer(20),new Integer(26),new Integer(28),
new Integer(32),new Integer(40)}; //Array of Integer Objects
private static List aList = new ArrayList(Arrays.asList(values));
public static boolean contains(List aList, Integer anInteger)
{
if (aList.size() == 0) return false;
return contains(aList, anInteger, 0, aList.size()-1);
}
public static boolean contains(List aList, Integer anInteger, int beginIndex, int endIndex)
{
if (beginIndex == endIndex)
return aList.get(beginIndex).equals(anInteger);
if (anInteger.intValue()<((Integer)aList.get(beginIndex)).intValue())
return false;
if (anInteger.intValue()>((Integer)aList.get(beginIndex)).intValue())
return false;
int middleIndex = (endIndex + beginIndex)/2;
Integer middleInteger = (Integer)aList.get(middleIndex);
if (anInteger.intValue() < middleInteger.intValue())
return contains(aList,anInteger,beginIndex,middleIndex);
return contains(aList,anInteger,middleIndex+1, endIndex);
}
public static void main(String args[])
{
System.out.print(contains(aList,new Integer(16)));
//here, you cannot send "16" as an argument for contains(). you must create an integer object because that's how YOU defined your contains() function!
}
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|