我正在使用 9x9 二维数组的 slice 制作一个简单的数独游戏。我仍然刚开始使用 Golang 并且有一些 C++ 经验。我不断收到错误消息“无法将数独 [0:9][0](类型 [9]int)用作赋值中的类型 []int”。
var row1 []int = 数独[0][0:9] 该行正确地获取了二维数组第一行的值并将它们放入 row1 slice 中,但是使用 var col1 []int = Sudoku[0:9][0] 会导致上面的错误消息。我能做什么?提前致谢!
例如,
package main
import "fmt"
func main() {
var Sudoku [9][9]int
fmt.Println(Sudoku)
var row1 []int = Sudoku[0][0:9]
fmt.Println(row1)
var col1 []int = Sudoku[0:9][0]
fmt.Println(col1)
}
Playground :https://play.golang.org/p/Jk6sqqXR5VE
10:6: cannot use Sudoku[0:9][0] (type [9]int) as type []int in assignment
最佳答案
TLDR:在 Go 中,数组和 slice 并不完全相同。您不能将 slice 变量分配给数组值。
详细信息:
我们从 Sudoku [9][9]int 开始,变量是一个数组。
有效行var row1 []int = Sudoku[0][0:9]:
Sudoku[0] 返回 Sudoku 索引 0 处的元素,它是一个数组 [9]int。假设我们称这个临时结果为 temp
然后 temp[0:9] 返回 temp 中索引 0 和 9(不包括)之间的元素 slice 。这就是为什么您可以将其分配给 slice 变量 row1
现在麻烦行 var col1 []int = Sudoku[0:9][0]。根据命名,我猜你的意图是返回 Sudoku 数组的第一列
?不幸的是,这就是发生的事情:
Sudoku[0:9] 返回 Sudoku 的索引 0 和 9(不包括)之间的元素 slice 。因为 Sudoku 中的每个元素都是一个数组,所以这个 temp 值实际上包含 Sudoku 中类型为 [][9 的所有行]int.
现在 temp[0] 返回 temp 的第一个元素,它实际上只是 Sudoku 的第一行和类型数组 [9]int。因此,当您尝试将其分配给 []int ( slice )
所以如果 Sudoku 是一个矩阵
1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2
...
9 9 9 9 9 9 9 9 9
即使 Go 不提示输入,返回值也将是 [1 1 1 1 1 1 1 1 1] 而不是 [1 2 3 4 5 6 7 8 9 ]
关于arrays - 无法对二维数组的列进行 slice "cannot use Sudoku[0:9][0] (type [9]int) as type []int in assignment",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50029237/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我怎样才能完成http://php.net/manual/en/function.call-user-func-array.php在ruby中?所以我可以这样做:classAppdeffoo(a,b)putsa+benddefbarargs=[1,2]App.send(:foo,args)#doesn'tworkApp.send(:foo,args[0],args[1])#doeswork,butdoesnotscaleendend 最佳答案 尝试分解数组App.send(:foo,*args)
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
通过rubykoans.com,我在about_array_assignment.rb中遇到了这两段代码你怎么知道第一个是非并行赋值,第二个是一个变量的并行赋值?在我看来,除了命名差异之外,代码几乎完全相同。4deftest_non_parallel_assignment5names=["John","Smith"]6assert_equal["John","Smith"],names7end45deftest_parallel_assignment_with_one_variable46first_name,=["John","Smith"]47assert_equal'John