从字符串数组中删除最后一项 [英] Deleting Last Item from Array of String

查看:74
本文介绍了从字符串数组中删除最后一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个简单的二十一点游戏项目.首先,我创建纸牌数组:

I'm working on a simple blackjack game project. Firstly I create the array of cards:

string[] deck = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", };

比我乘以4并得到 deckNumber :

newDeck = Enumerable.Repeat(deck, deckNumber*4).SelectMany(x => x).ToArray();

当我想从阵列中删除最后一张卡时,请应用以下方法:

when I want to delete last card from array I apply this:

newDeck = newDeck.Where(w => w != newDeck.Last()).ToArray();

所以问题是代码不会从数组中删除最后一个元素,行为就像有多个数组,并从它们中删除了所有最后一个元素.例如一个甲板:

so the problem is that code doesn't removes last item from array, acts like there are multiple arrays and removes all last elements from them. For example with one deck:

cards: 2 3 4 5 6 7 8 9 10 J Q K A 2 3 4 5 6 7 8 9 10 J Q K A 2 3 4 5 6 7 8 9 10 J Q K A 2 3 4 5 6 7 8 9 10 J Q K A 

当我应用我的删除命令时,它变成:

when I apply my remove command it become:

cards: 2 3 4 5 6 7 8 9 10 J Q K 2 3 4 5 6 7 8 9 10 J Q K 2 3 4 5 6 7 8 9 10 J Q K 2 3 4 5 6 7 8 9 10 J Q K 

它从数组中删除所有A.但是我只想从整个数组中删除最后一项.有什么问题我该如何解决?

it removes all the A's from array. But I want to remove only the last item from whole array. Whats the problem how can I solve this?

推荐答案

要仅删除最后一个元素,请使用以下方法:

To remove just the last element use this:

newDeck = newDeck.Take(newDeck.Count() - 1).ToArray();

您的解决方案将删除所有等于最后一个元素的元素.对于字符串,这意味着它将删除所有等于 A

Your solution removes all Elements that are equal to the last element. For a string, this means, it removes all elements equal to A

这篇关于从字符串数组中删除最后一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆