是否可以将通用对象转换为字符串之类的东西?
我认为转换泛型类似于普通的对象转换,如:
public String tempString;
public E genericObject;
tempString = ((String) genericObject);
Run Code Online (Sandbox Code Playgroud)
但是,在编译时我仍然会遇到不兼容的类型错误.
required: String
found: E
Run Code Online (Sandbox Code Playgroud) 您好我正在使用StringBuffer和StringBuilder,这是我写的一个小程序,以帮助我理解它是如何工作的.然而,一些奇怪的东西与Stringbuffer无关,而是我的for循环中的多if if else语句.
我的小代码:
public class c
{
public static void main(String args[])
{
StringBuffer infixB = new StringBuffer();
String infix = "3/8";
for(int i = 0; i < infix.length(); i++)
{
infixB.append(infix.charAt(i));
}
infixB.append(')');
System.out.println("Your current infix expression: "+infixB.toString());
//go through the 'infixB' 1 position at a time
for(int i = 0; i < infixB.length(); i++)
{
if(infixB.charAt(i) == '3')
{
System.out.println("Digit 3 at: " +i);
}
else if(infixB.charAt(i) == '/')
{
System.out.println("Operator at: "+i);
}
else if(infixB.charAt(i) == …Run Code Online (Sandbox Code Playgroud)