小编One*_*ror的帖子

检查变量的类型是否是Python中的特定类型

我想检查变量的类型是否是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)

我怎样才能做到这一点 ?

python

4
推荐指数
1
解决办法
3180
查看次数

突出显示在框架中打开的文本文件的一些单词

我在一个框架中打开一个文件,我想强调一些单词.据我所知,我需要遍历文件的内容.如何遍历内容以及我可能用于突出显示的相关属性是什么?


更新:我的代码似乎有点像这样


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 user-interface swing jeditorpane

3
推荐指数
1
解决办法
2311
查看次数

在Java中创建Object类的对象

在Java中,每个类都隐式扩展了Object类.那么,这是否意味着我们可以创建Object类的对象?


public static void main(String[] args) {

Object ob=new Object();
    // code here ....
 }
Run Code Online (Sandbox Code Playgroud)

当我尝试它时,它编译并成功运行.在这种情况下,有人可以解释我们何时通常创建Object类的对象?

java

3
推荐指数
1
解决办法
122
查看次数

结肠的意义:在Ruby中

可能重复:
了解Ruby中的符号Ruby
中的冒号运算符是什么?

我真的觉得这很天真,但我会继续问:

:红宝石的重要性是什么?

我已经看到它被用于许多类似params[:id]或类似的地方x < :length.

ruby

3
推荐指数
1
解决办法
486
查看次数

Java中没有重复的字符串的所有可能组合

我知道这个问题之前已经在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).

java string

3
推荐指数
1
解决办法
5675
查看次数

无法在Prolog中定义谓词

我刚开始学习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.这里的错是什么?

prolog

3
推荐指数
1
解决办法
2297
查看次数

当我们点击不同的按钮时,其内容会发生变化的JFrame

我在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,当我们点击不同的按钮时,其内容会发生变化.我怎样才能做到这一点?

java swing jframe cardlayout

3
推荐指数
1
解决办法
7945
查看次数

使用Stacks Java的Infix到Postfix

我正在尝试编写一个程序来将中缀表达式转换为后缀表达式.

我使用的算法如下:

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)

java algorithm stack data-structures

3
推荐指数
1
解决办法
5万
查看次数

代表列表数组

这似乎是一个基本问题,但它确实让我很困惑.我试图表示图的邻接列表.我有两个问题:

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)

最后两种方法有什么问题?我认为第一个更正确.

java arrays list

3
推荐指数
1
解决办法
1084
查看次数

使用 JQuery 动态重复和删除表单元素

我有一个带有附加添加和删除按钮的表单。单击这些按钮后,相同的表单行将被重复或完全删除。

    <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)

javascript jquery

3
推荐指数
1
解决办法
1956
查看次数