标签: variable-assignment

将单词插入字典中正确位置的最快方法

目前,我只是将单词插入到字典(ArrayList<String>)中,然后对字典进行排序,如下所示:

dictionary.add(newWord);
Collections.sort(dictionary, new Comparator<String>(){
    public int compare(String s1, String s2) {
        return s1.compareToIgnoreCase(s2);
    }        
});
Run Code Online (Sandbox Code Playgroud)

我试图确定这种方式是否真的是最好的.当然,另一种方法是在字典中找到正确的点,然后在那里插入单词.问题是,我无法想出一种有效/可靠的方法来在字典中找到这一点.我脑子里浮现出一些想法,但把纸笔放在纸上真是太棘手了.

如果您对如何操作有所了解,请不要发布任何大量的代码答案.这是作业的一部分,所以你可以告诉我你是怎么做的,而不是张贴代码吗?(也许是伪代码?)

谢谢.

java dictionary variable-assignment

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

如何将对象分配给smarty模板?

我在PHP中创建了一个模型对象

class User {
  public $title;

  public function changeTitle($newTitle){
    $this->title = $newTitle; 
  }
}
Run Code Online (Sandbox Code Playgroud)

如何通过分配对象来公开smarty中的User对象的属性?

我知道我能做到这一点

$smarty->assign('title', $user->title);
Run Code Online (Sandbox Code Playgroud)

但我的对象有超过20多个属性.

请指教.

编辑1

以下对我不起作用.

$smarty->assign('user', $user);
Run Code Online (Sandbox Code Playgroud)

要么

$smarty->register_object('user', $user);
Run Code Online (Sandbox Code Playgroud)

然后我试着 {$user->title}

一切都没有出来.

编辑2

我目前只想在smarty模板中输出对象的公共属性.对不起,如果我把任何一个与功能混淆.

谢谢.

php smarty variable-assignment viewmodel

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

python变量

我有一个简单的问题.我从文件中读取一个字符串并存储在一个列表中(名为:formula).然后我将(公式)存储在另一个变量(fstore = formula)中以便以后保存.然后我对公式进行了修改,事实证明,fstore也在改变.为什么会这种情况继续发生?我希望fstore保持不变.我没有在程序中的任何其他地方使用fstore.我的作业(fstore =公式)错了吗?

松鸦

python variables variable-assignment

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

C struct字段赋值会覆盖另一个字段

我正在写一个有趣的幻想足球选秀计划.

我遇到了一个奇怪的问题.我为一个struct字段分配了一个值,但这也发生了,但它也将该值赋给了另一个字段struct.为凌乱的调试printf语句道歉.

我显然不了解struct字段分配.

码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define TRUE 1

int QB_count = 0;
int RB_count = 0;
int WR_count = 0;
int TE_count = 0; 
int DEF_count = 0;

struct Player { 
    char *name;
    char *position;
    int age;
    int bye_week;
};

int get_name (struct Player *drafted) {
    char name[20];
    fputs("Enter Player Name: ", stdout);
    fflush(stdout);
    if (fgets(name, sizeof name, stdin) != NULL){
        char *newline = strchr(name, '\n');
        if …
Run Code Online (Sandbox Code Playgroud)

c struct field variable-assignment

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

innerHTML显示函数,而不是函数的返回值

试图干掉我写的一些旧的javascript.

测试()

function test() {
    var output = function() {
        return ajaxPost("test.php", "testvar=bananas");
    }
    document.getElementById("main").innerHTML = output;
}
Run Code Online (Sandbox Code Playgroud)

ajaxPost()

function ajaxPost(file,stuff) {
    var xmlhttp;
    var actionFile = file;
    var ajaxVars = stuff;

    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            return xmlhttp.responseText;
        } else {
            // Waiting...
        }
    }

    xmlhttp.open("POST", actionFile, true);

    //Send the proper …
Run Code Online (Sandbox Code Playgroud)

javascript anonymous-function variable-assignment

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

使用bash,如何使用echo将整数赋值给变量

我想更好地理解bash,因为我显然很可怕...

我正在尝试生成一系列恒定宽度的整数,但随后测试它们以针对特定值执行特殊操作.像这样:

  for n in $(seq -w 1 150)
  do
    # The next line does not work: doit.sh: line 9: XX: command not found
    #decval= $( echo ${n} | sed 's/^0//g' | sed 's/^0//g' )
    #if [[ ${decal} -eq 98 ]] ; then
    if [[ $( echo ${n} | sed 's/^0//g' | sed 's/^0//g' ) -eq 98 ]] ; then
      echo "Do something different for 98"
    elif [[ $( echo ${n} | sed 's/^0//g' | sed 's/^0//g' ) -eq 105 …
Run Code Online (Sandbox Code Playgroud)

bash variable-assignment

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

Perl:直接为散列分配标量

在查看一些Perl代码时,我遇到了将chr的输出直接赋值给hash:

local %str = chr(shift);
Run Code Online (Sandbox Code Playgroud)

感到困惑的是代码工作正常,这让我意识到以下几点:

perl -le 'my $jurso = 23; print $jurso;'
23

perl -le 'my %jurso = 23; print %jurso;'
23

perl -le 'my @jurso = 23; print @jurso;'
23
Run Code Online (Sandbox Code Playgroud)

我期望将标量直接分配给散列或数组以导致错误.有人可以解释为什么jurso变量表现得像标量而不管使用的sigil?

谢谢.

perl hash scalar sigils variable-assignment

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

在分配字典中的值时,在Python中导致这种奇怪结果的原因是什么?

我最近将某些代码中的错误减少为以下行为的结果:

>>> arr = np.zeros(10)
>>> value = 0
>>> dictionary = {"key":[arr,value]}
>>> dictionary["key"][0]
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
>>> dictionary["key"][1]
0
>>> dictionary["key"][0]+=1
>>> dictionary["key"][1]+=1
>>> dictionary["key"][0]   
array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.])
>>> dictionary["key"][1]   
1
>>> arr
array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.])
>>> value
0
Run Code Online (Sandbox Code Playgroud)

导致:

>>> dictionary["key"][0] is arr
True
>>> dictionary["key"][1] is value 
False
Run Code Online (Sandbox Code Playgroud)

可能是一个愚蠢的问题,但是是什么导致了这个?

python dictionary numpy variable-assignment

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

Delphi简单数据类型等价

在Delphi中,如果X和Y是整数,我如何编写代码使得X和Y指向相同的内存位置?如果我说的话

Y := 3;
X := 6;
write(Y);
Run Code Online (Sandbox Code Playgroud)

输出将是6?据我所知,Delphi中简单类型赋值的默认行为是值而不是引用.

delphi reference variable-assignment

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

在声明变量时进行初始化与在c中的单独行上进行比较

请注意,我正在执行此奇怪的任务不是因为我不清楚语法或对c还是陌生的。

我只是在尝试如果将一个int分配给一个数组会发生什么:

int a = {1, 2, 3};
Run Code Online (Sandbox Code Playgroud)

为此,我收到以下警告:

 warning: excess elements in scalar initializer
  int a = {1, 2, 3};
              ^
warning: excess elements in scalar initializer
  int a = {1, 2, 3};
                 ^
Run Code Online (Sandbox Code Playgroud)

当我打印a的内容时,我得到1。

但是当我这样做时:

int a;
a = {1, 2, 3};
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

error: expected expression before ‘{’ token
  a = {1, 2, 3};
      ^
Run Code Online (Sandbox Code Playgroud)

尽管我知道数组不是要分配给int变量的,但以上结果使我提出了疑问:

  • 之间的差异int x; x = ...和int x = ...

和

  • 究竟是什么引起警告或错误消息?

c arrays initialization variable-assignment

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