Tag Archives: JAVA Error

How to Solve JAVA Error: “error: bad operand types for binary operator ”

When doing this topic today (142. O (1) check power of 2), I encountered an error “bad operate types for binary operator ‘& amp;”

First, paste the code:

public class Solution {
    /**
     * @param n: An integer
     * @return: True or false
     */
    public boolean checkPowerOf2(int n) {
        // write your code here
         if(n<=0)
           return false;
         return (n&(n-1)==0)?true:false;
    }
}

Error:

At first, I guessed that it was an operator problem, but I also ruled it out. I always feel that there is something wrong with the “true” and “false” behind, but I can’t find it. Later, I learned that it was really a matter of priority, which might be affected by the assignment (=) operator; The priority of “=” is higher than “=”, which is actually the opposite. The identity operator takes precedence over the bitwise operator, which results in “& amp;” The left is int type, the right is boolean type, sure enough, the basic things still can’t be ignored

So just put “return (n & amp( n-1)==0)? true:false; ” Change to “return ((n & amp( n-1))==0)? true:false; ” That’s it

 

How to Solve Java Error: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

Project requirements: convert a comma separated string into a list, and then clone the list and assign it to a new NEWLIST. An error was reported in the title during execution

Text:

Original code

public List<String> test() {
    String str = "a,b,c";
    List<String> list = Arrays.asList(str.split(",")); 
    //Only ArrayList and LinkedList have clone method, the purpose of clone is to operate on the list afterwards without changing the value of the original list.
    ArrayList<String> tempList = (ArrayList<String>)list;
    List<String> newList = (List<String>)tempList.clone;
    return newList;
}

Correction

//Just replace the code in the replacement line
ArrayList<String> tempList= new ArrayList<>(list);

Java error: No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

 

public class Close {
	public static void main(String[] args){
		try{
		
			Resource res = new Resource();//This compilation prompts an error
			res.dosome();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	 class Resource implements AutoCloseable{
		void dosome(){
			System.out.println("Do something");
		}
		public void close() throws Exception{
			System.out.println("Resources are closed");
		}
	}
}

Error content: no enclosing instance of type close is accessible. Must qualify the allocation with an enclosing instance of type close (e.g. X.New a() where x is an instance of close)

that is, if there is no accessible instance of inner class E, an appropriate instance of inner class E must be assigned (such as X.New a (), X must be an instance of E.) looking at this prompt, I wonder why I have instantiated this class with new

It turns out that the internal class I wrote is dynamic, that is, it starts with public class. The main program is public static class main. In Java, static methods in a class cannot directly call dynamic methods. Only if an internal class is modified to a static class, then the member variable and member method of the class can be invoked in a static class. Therefore, without making other changes, the simplest solution is to change the public class to public static class.