operator == cannot be applied to int,java.lang.Integer
807607Jan 8 2007 — edited Jan 10 2007import java.util.ArrayList;
public class intObject
{
public static void main(String [] args)
{
// Create the ArrayList 5 7 3 4 9
// Produce the output below through appropriate method calls
// ==================================
// The ArrayList contains 5 7 3 4 9
// The location of 5 is 0
// The max is 9
// The min is 3
// The sum is 28
// The average is 5.6
// After adding 1 in location 2 and
// removing values in locations 4 and 0,
// The ArrayList contains 7 1 3 9
// The location of 5 is -1
// The max is 9
// The min is 1
// The sum is 20
// The average is 5.0
ArrayList list = new ArrayList();
list.add( new Integer(5) );
list.add( new Integer(7) );
list.add( new Integer(3) );
list.add( new Integer(4) );
list.add( new Integer(9) );
System.out.print("The ArrayList contains ");
display(list);
System.out.println("The location of 5 is " + search(list, 5) );
//System.out.println("The max is " + max(list) );
//System.out.println("The min is " + min(list) );
System.out.println("The sum is " + sum(list) );
/*System.out.println("The average is " + average(list) );
list.add( 2, new Integer(1) );
list.remove(4);
// NOTE: ArrayList objects can be dynamically resized
list.remove(0);
System.out.print("The ArrayList contains ");
display(list);
System.out.println("The location of 5 is " + search(list, 5) );
System.out.println("The max is " + max(list) );
System.out.println("The min is " + min(list) );
System.out.println("The sum is " + sum(list) );
System.out.println("The average is " + average(list) );*/
}
private ArrayList ar= new ArrayList();
public static void display(ArrayList ar)
{
for(int i=0;i<ar.size();i++)
{
System.out.println(ar.get(i));
}
}
public static int sum(ArrayList ar)
{
int sum=0;
for(int i =0; i<ar.size();i++)
{
Integer x = (Integer)ar.get(i);
sum += x.intValue();
}
return sum;
}
public static int search(ArrayList ar, int num )
{
int location=0;
for (int i = 0; i< ar.size(); i++)
{
if(num==((Integer)ar.get(i)))
{
location=i;
}
}return location;
}
public static int max(ArrayList ar)
{
int greatest = 0;
for(int i =0; i <ar.size();i++)
{
if(greatest<(Integer)ar.get(i))
greatest=(Integer)ar.get(i);
}return greatest;
}
public static int min(ArrayList ar)
{
int smallest = 99999999;
for(int i =0; i <ar.size(); i++)
{
if(smallest>(Integer)ar.get(i))
smallest=(Integer)ar.get(i);
}return smallest;
}
}
I never got these errors while i was using the school computer but when I tried doing the program at home i got a whole bunch of errors.
public static int search(ArrayList ar, int num )
{
int location=0;
for (int i = 0; i< ar.size(); i++)
{
if(num==((Integer)ar.get(i)))//operator == cannot be applied to int,java.lang.Integer
{
location=i;
}
}return location;
}
public static int max(ArrayList ar)
{
int greatest = 0;
for(int i =0; i <ar.size();i++)
{
if(greatest<(Integer)ar.get(i))//operator == cannot be applied to int,java.lang.Integer
greatest=(Integer)ar.get(i);
}return greatest;
}
can someone explain to me what's wrong with these problems?