Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 14th, 2005, 1:08 AM   #1
jch02140
Newbie
 
Join Date: Jul 2005
Posts: 15
Rep Power: 0 jch02140 is on a distinguished road
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));
		
	}
}
jch02140 is offline   Reply With Quote
Old Aug 14th, 2005, 1:26 PM   #2
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
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
OpenLoop is offline   Reply With Quote
Old Aug 14th, 2005, 1:30 PM   #3
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
.. 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
stevengs is offline   Reply With Quote
Old Aug 14th, 2005, 1:37 PM   #4
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Quote:
Originally Posted by stevengs
.. the wrapper methods simply need to call the wrapped class's methods (seems kinda pointless though )
well, i also read that the conversion takes place automatically in JDK 5.0 so you don't have to deal with all this none sense.
OpenLoop is offline   Reply With Quote
Old Aug 14th, 2005, 2:38 PM   #5
jch02140
Newbie
 
Join Date: Jul 2005
Posts: 15
Rep Power: 0 jch02140 is on a distinguished road
Question

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).
		
	}
}
jch02140 is offline   Reply With Quote
Old Aug 14th, 2005, 2:42 PM   #6
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
Quote:
Originally Posted by jch02140
I have changed the code but I still get an error message when I compile it
... hmmm.... are you just gone let the suspense build up or what? Just because we know some programmin' doesn't make us omniscient. And most of us are not psychic. Care to let us in on yer little secret?
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty
stevengs is offline   Reply With Quote
Old Aug 14th, 2005, 3:08 PM   #7
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
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!
        }
}
Still, i don't know if the program is doing its job I just fixed the compile problems
OpenLoop is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC