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

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

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条回答 默认 最新

  • dongxianji0968
    dongxianji0968 2018-01-06 14:37
    已采纳

    copy_numbers is of size 0, just as you initialized it.

    If I add the following before your first for loop:

    fmt.Println(numbers)
    fmt.Println(copy_numbers)
    

    I get:

    [5 2 3 5 3]
    []
    

    the copy builtin copies up to the length of either argument, as mentioned in the docs:

    Copy returns the number of elements copied, which will be the minimum of len(src) and len(dst).

    You should initialize copy_numbers to be of the same length as numbers

      copy_numbers := make([]int, len(numbers))
    
    点赞 评论
  • dongtuji0992
    dongtuji0992 2018-01-06 14:34

    len(copy_numbers) is zero because copy will only copy min(len(src), len(dst)) elements and copy_numbers length is zero.

    Use this line to define a bigger copy_numbers so copy does what you want:

    copy_numbers[] := make([]int, len(numbers))
    
    点赞 评论
  • drl9940
    drl9940 2018-01-06 14:38

    From the documentaion:

    The copy built-in function copies elements from a source slice into a destination slice. (As a special case, it also will copy bytes from a string to a slice of bytes.) The source and destination may overlap. Copy returns the number of elements copied, which will be the minimum of len(src) and len(dst).

    in your case, the destination is copy_numbers, which is an uninitialized slice. To initialize it, and have enough space to hold a copy of numbers you can simply do

    copy_numbers := make([]int, len(numbers)

    点赞 评论
  • duanhuang7591
    duanhuang7591 2018-01-06 14:39

    The issue is with the way you've used copy. From the docs:

    Copy returns the number of elements copied, which will be the minimum of len(src) and len(dst).

    The manner in which you are creating copy_numbers initializes it with a zero length.

    var copy_numbers []int // len(copy_numbers) == 0
    

    This causes copy to not copy a single element from numbers.

    To fix this, you can change copy_numbers initialization to:

    copy_numbers := make([]int, len(numbers))
    

    The rest of the code should work.

    点赞 评论

相关推荐