第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何編輯隱藏在遞歸結(jié)構(gòu)中的數(shù)組

如何編輯隱藏在遞歸結(jié)構(gòu)中的數(shù)組

Go
夢(mèng)里花落0921 2023-07-10 10:55:55
我有這個(gè)結(jié)構(gòu)(注意它是遞歸的?。簍ype Group struct {  Name string  Item []string  Groups []Group}我想將一個(gè)字符串附加到Item深埋在 Group 數(shù)組的層次結(jié)構(gòu)中的數(shù)組中。關(guān)于這個(gè)新項(xiàng)目的路徑,我所掌握的唯一信息是它所在組的名稱。假設(shè)路徑是“foo/bar/far”。我想修改 bar 而不覆蓋 foo、bar 或“root”數(shù)組?;旧?,我想編寫一個(gè)函數(shù),返回一個(gè)新的 Group 變量,該變量與原始變量相同,但附加了新字符串。到目前為止我已經(jīng)嘗試過以下方法:循環(huán)遍歷包含路徑的所有組名稱的數(shù)組,如果它們位于當(dāng)前組中,則將當(dāng)前組變量設(shè)置為該新組。循環(huán)完成后,將字符串附加到數(shù)組并返回當(dāng)前組。當(dāng)然,唯一的問題是根組的其余部分被刪除并替換為新的、修改過的組。代碼:func in(item string, array []Group) (bool, int) {        for i, elem := range array {                if item == elem.Name {                        return true, i                } else {                        continue                }        }        return false, 0}func addItem(list Group, newItem string, path string) Group {        var currentGroup Group = list        if path == "" {                currentGroup.Items = append(currentGroup.Items, newItem)        } else {                for _, elem := range strings.Split(path, "/") {                        in, index := in(elem, currentGroup.Groups)                        if in {                                currentGroup = currentGroup.Groups[index]                        }                }                currentGroup.Items = append(currentGroup.Items, newItem)        }        return currentGroup}
查看完整描述

1 回答

?
茅侃侃

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超21個(gè)贊

我想您可以將組作為指針傳遞給 addItem 函數(shù),并忽略該函數(shù)的返回值


有一點(diǎn)像


func addItem(list *Group, newItem string, path string) Group {

? ? var currentGroup *Group = list

? ? if path == "" {

? ? ? ? currentGroup.Item = append(currentGroup.Item, newItem)

? ? } else {

? ? ? ? for _, elem := range strings.Split(path, "/") {

? ? ? ? ? ? in, index := in(elem, currentGroup.Groups)

? ? ? ? ? ? if in {

? ? ? ? ? ? ? ? currentGroup = &currentGroup.Groups[index]

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? currentGroup.Item = append(currentGroup.Item, newItem)

? ? }


? ? return *currentGroup

}

查看完整回答
反對(duì) 回復(fù) 2023-07-10
  • 1 回答
  • 0 關(guān)注
  • 121 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)