亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Go:數組超出索引恐慌錯誤

Go:數組超出索引恐慌錯誤

Go
慕絲7291255 2021-06-18 03:24:57
我正在實施排序,但在 Go 語言中不斷收到索引綁定錯誤。我的代碼如下 func My_Partition(container []int, first_index int, last_index int) int {      var x int = container[last_index]      i := first_index - 1      for j := first_index; i < last_index; j++ {           if container[j] <= x {                i += 1                my_Swap(&container[i], &container[j])           }      }      my_Swap(&container[i+1], &container[last_index])      return i+1 }我在“if container[j] <= x”行中收到錯誤,提示panic:運行時錯誤:索引超出范圍    main.My_Partition(0x2101b20c0, 0x7, 0x7, 0x0, 0x6, ...)/Path/main.go:34 +0xff有人有想法嗎?我的交換功能在下面 func my_Swap(a *int, b *int) {      temp := *a      *a = *b      *b = temp }但我不認為交換是問題所在。
查看完整描述

1 回答

?
莫回無

TA貢獻1865條經驗 獲得超7個贊

你有一個錯字:

for j := first_index; i < last_index; j++ {

應該:

for j := first_index; j < last_index; j++ {

很容易犯的錯誤:-)


查看完整回答
反對 回復 2021-06-28
  • 1 回答
  • 0 關注
  • 295 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號