doubei2340
2018-01-06 14:25
浏览 34
已采纳

我在运行以下代码时出现索引超出范围错误

i having the following code to print the indexes of an array which contains similar values.

but on running this code getting panic: indexes out of range run time error

why i am getting this

package main

import "fmt"

func main()  {
    var numbers[] int = [] int {5,2,3,5,3}
    var copy_numbers[] int
    var count [5] int
    var i,j int
    copy(copy_numbers,numbers)
    for i=0;i<len(numbers);i++ {
        for j=0;j<len(numbers) ;j++  {
            if numbers[i]==numbers[j] {
                count[i]++;
            }
        }
    }
    fmt.Printf("%d",count)

    for i=0;i<len(numbers) ;i++  {
        fmt.Println("hello")
        if count[i]>1{
            for j=0;j<len(numbers) ;j++  {
                if numbers[i]==copy_numbers[j] {

                    fmt.Printf("%d",j)

                }
            }
            fmt.Println("")
        }
    }

fmt.Printf("%d",count)

}
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

4条回答 默认 最新

相关推荐 更多相似问题