我想检查变量的类型是否是Python中的特定类型。例如-我想检查 varx是否是 int 。
>>x=10
>>type(x)
<type 'int'>
Run Code Online (Sandbox Code Playgroud)
但我如何比较它们的类型。我尝试过这个,但似乎不起作用。
if type(10)== "<type 'int'>":
print 'yes'
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点 ?
我在一个框架中打开一个文件,我想强调一些单词.据我所知,我需要遍历文件的内容.如何遍历内容以及我可能用于突出显示的相关属性是什么?
更新:我的代码似乎有点像这样
private JEditorPane editorpane;
JScrollPane editorScrollPane;
public TextEditor()
{
editorpane = new JEditorPane();
editorpane.setEditable(false);
if (filename != null)
{
try
{
File file = new File(filename);
editorpane.setPage(file.toURI().toURL());
}
catch (IOException e)
{
e.printStackTrace();
System.err.println("Attempted to read a bad file ...");
}
}
else
{
System.err.println("File name is wrong");
}
add(editorpane);
}
Run Code Online (Sandbox Code Playgroud) 在Java中,每个类都隐式扩展了Object类.那么,这是否意味着我们可以创建Object类的对象?
public static void main(String[] args) {
Object ob=new Object();
// code here ....
}
Run Code Online (Sandbox Code Playgroud)
当我尝试它时,它编译并成功运行.在这种情况下,有人可以解释我们何时通常创建Object类的对象?
可能重复:
了解Ruby中的符号Ruby
中的冒号运算符是什么?
我真的觉得这很天真,但我会继续问:
:红宝石的重要性是什么?
我已经看到它被用于许多类似params[:id]或类似的地方x < :length.
我知道这个问题之前已经在stackoverflow上得到了解答 ,但是我要求这不要告诉我正确的代码,而是因为我想知道我做错了什么.
public static void printCombinations(String str){
printCombinations(str, 0, str.length()-1);
}
public static void printCombinations(String str,int k,int n){
if(k == n)
System.out.println(str);
else {
for(int i=k;i<n;i++){
String tmp=modifyString(str,i,k);
printCombinations(tmp,k+1,n);
modifyString(str,i,k);
}
}
}
public static String modifyString(String str,int x,int y){
// for swapping characters inside a string
char arr[]=str.toCharArray();
char t= arr[x];
arr[x]=arr[y];
arr[y]=t;
String s= new String(arr);
return s;
}
Run Code Online (Sandbox Code Playgroud)
我把这个函数称为printCombinations(s).
我刚开始学习Prolog,如果这有点天真,或者说很天真,请原谅我.我试图定义这个谓词
| ?- times(M,N,Product) :- Product is M*N.
Run Code Online (Sandbox Code Playgroud)
这给了我这个错误
uncaught exception: error(existence_error(procedure,(:-)/2),top_level/0)
Run Code Online (Sandbox Code Playgroud)
我正在使用GNU Prolog.这里的错是什么?
我在Java's Swing这里用来制作UI应用程序.我有一个创建了一个JFrame,带有一些按钮.当我点击这个按钮时,我想要一个在这个地方有一些不同内容的新JFrame.但是,我不想在这里加载新的JFrame.
我知道一种方法是actionPerformed(ActionEvent obj)在第一个JFrame中按钮的方法中将第二个JFrame的可见性设置为True .但它再次加载一个新的JFrame,我不希望这样.
public class FirstUI extends JFrame {
JButton but1;
public FirstUI(){
but1= new JButton("Click here");
add(but1);
XYZ obj= new XYZ():
but1.addActionListener(obj);
}
public class XYZ implements ActionListener{
public void actionPerformed(ActionEvent obj1){
// WHAT TO DO HERE
}
}
}
Run Code Online (Sandbox Code Playgroud)
我只想要一个JFrame,当我们点击不同的按钮时,其内容会发生变化.我怎样才能做到这一点?
我正在尝试编写一个程序来将中缀表达式转换为后缀表达式.
我使用的算法如下:
1. Create a stack
2. For each character t in the expression
- If t is an operand, append it to the output
- Else if t is ')',then pop from the stack till '(' is encountered and append
it to the output. do not append '(' to the output.
- If t is an operator or '('
-- If t has higher precedence than the top of the stack, then push t
on to the stack.
-- …Run Code Online (Sandbox Code Playgroud) 这似乎是一个基本问题,但它确实让我很困惑.我试图表示图的邻接列表.我有两个问题:
public class Graph
{
private final int V;
private List<Integer>[] adj;
public Graph(int V)
{
this.V = V;
this.adj = (List<Integer>[]) new LinkedList[V]; // this works
}
}
Run Code Online (Sandbox Code Playgroud)
问题1:当我执行以下操作时,它会出错
Array type expected; found: 'java.util.LinkedList<java.lang.Integer>'
this.adj = (List<Integer>[]) new LinkedList<Integer>()[V];
Run Code Online (Sandbox Code Playgroud)
我正在创建一个Integer数组列表,对吧?
问题2:当我这样做时,它再次给出了一个错误,说明通用数组的创建:
this.adj = (List<Integer>[]) new LinkedList<Integer>[V];
Run Code Online (Sandbox Code Playgroud)
最后两种方法有什么问题?我认为第一个更正确.
我有一个带有附加添加和删除按钮的表单。单击这些按钮后,相同的表单行将被重复或完全删除。
<script>
function addFormElements(current) {
$(current).parent().parent().append($(current).parent().parent().html());
}
function removeFormElements(current) {
$(current).parent().parent().remove();
}
</script>Run Code Online (Sandbox Code Playgroud)
<div class="container">
<div class="row justify-content-center align-items-center">
<div class="col-10 col-md-10 col-lg-8">
<form>
<div class="form-row" style="padding-top: 50px;" id="someId">
<div class="form-group col-md-4">
<select class="selectpicker form-control single-select" placeholder="Select an Option..." required>
<option></option>
<option>Mustard</option>
<option>Ketchup</option>
<option>Barbecue</option>
</select>
</div>
<div class="form-group col-md-6">
<input type="password" class="form-control" placeholder="">
</div>
<div class="form-group col-md-1">
<button type="button" class="btn btn-success" onclick="addFormElements(this)">+</button>
</div>
<div class="form-group col-md-1">
<button type="button" class="btn btn-danger" onclick="removeFormElements(this)">-</button>
</div>
</div>
<div class="form-row" style="padding-top: 50px;">
<button type="button" class="btn …Run Code Online (Sandbox Code Playgroud)java ×6
swing ×2
algorithm ×1
arrays ×1
cardlayout ×1
javascript ×1
jeditorpane ×1
jframe ×1
jquery ×1
list ×1
prolog ×1
python ×1
ruby ×1
stack ×1
string ×1