C# 编写removeLast方法
情况: 我是c#新手,目前正在学习诀窍,同时也在完善我的数据结构,我决定创建一个类,该类将对线性阵列执行多个功能,因为有人建议我应该从线性阵列开始,然后再处理圆形阵列 我的类当前提供的方法有:C# 编写removeLast方法,c#,arrays,C#,Arrays,情况: 我是c#新手,目前正在学习诀窍,同时也在完善我的数据结构,我决定创建一个类,该类将对线性阵列执行多个功能,因为有人建议我应该从线性阵列开始,然后再处理圆形阵列 我的类当前提供的方法有: 将项目添加到阵列的最前面位置 将项目添加到阵列的最后面位置 正在删除数组中的第一项 正在删除数组中的最后一项,//todo 清除其值的当前数组列表 向用户显示数组列表内容 问题: 我很难构造一个删除数组最后一项的方法,我已经在线查看了,预写的方法似乎很复杂,由于我的经验,我无法理解它们。我意识到其他人
- 将项目添加到阵列的最前面位置
- 将项目添加到阵列的最后面位置
- 正在删除数组中的第一项
- 正在删除数组中的最后一项,//todo
- 清除其值的当前数组列表
- 向用户显示数组列表内容
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace arraydatastructuresactual
{
public class LinearArrayList
{
private int count; //how many numbers currently stored
private int[] values; //array to hold values entered into list
//constructors
/// <summary>
/// //creates a linear array that can hold max values
/// </summary>
/// <param name="max"></param>
public LinearArrayList(int max)
{
count = 0;
values = new int[max]; //makes the linear array as big as the max value
}
/// <summary>
/// //default constructor sets capacity to 10
/// </summary>
public LinearArrayList()
{
count = 0;
values = new int[10];
}
/// <summary>
/// returns true if list is empty
/// otherwise turns false
/// </summary>
/// <returns>true if empty otherwise false</returns>
public bool isEmpty()
{
return (count == 0);
}
/// <summary>
/// returns true if list is full
/// otherwise turns false
/// </summary>
/// <returns>true if full otherwise false</returns>
public bool isFull()
{
return (values.Length <= count);
}
/// <summary>
/// if not full adds value to the end of list
/// throws exception if list is fulll
/// </summary>
/// <param name="value">value to add to end of list</param>
public void addLast(int value) //adds an item to the last position in the array.
{
if (isFull())
{
throw new Exception("List Full");
}
else
{
values[count++] = value;
}
}
public void addFirst(int value) //Adds an item to the first position in the array.
{
if (isFull())
{
throw new Exception("List Full");
}
else
{
for (int i = count; i > 0; i--)
{
values[i] = values[i--];
}
values[0] = value;
count++;
}
}
public int removeFirst() //removes the first item from the array
{
if (isEmpty())
throw new Exception("List is Empty");
int value = values[0];
count--;
for (int i = 0; i < count; i++)
{
values[i] = values[i + 1];
}
return value;
}
public int removeLast() //todo //removes the last item from the array
{
if (isEmpty())
throw new Exception("List is Empty");
int value = values[0];
count--;
for (int i = count; i < count; i++)
{
values[i] = values[i + 1];
}
return value;
}
public void displayUI()//displays contents of list
{
}
public void destroy() //Empties The List
{
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间arraydatastructuresactual
{
公共类线性数组列表
{
private int count;//当前存储了多少个数字
private int[]values;//用于保存输入列表中的值的数组
//建设者
///
/////创建可容纳最大值的线性数组
///
///
公共线性RAYLIST(整数最大值)
{
计数=0;
values=new int[max];//使线性数组与最大值一样大
}
///
/////默认构造函数将容量设置为10
///
公共线性RAYLIST()
{
计数=0;
数值=新整数[10];
}
///
///如果列表为空,则返回true
///否则将变为错误
///
///如果为空,则为true,否则为false
公共图书馆是空的
{
返回(计数=0);
}
///
///如果列表已满,则返回true
///否则将变为错误
///
///如果完整则为true,否则为false
公共文件已满()
{
返回值(value.Length 0;i--)
{
值[i]=值[i--];
}
数值[0]=数值;
计数++;
}
}
public int removeFirst()//从数组中删除第一项
{
if(isEmpty())
抛出新异常(“列表为空”);
int值=值[0];
计数--;
for(int i=0;i
如果有人能分享我如何实现这一目标的经验,那么非常感谢,我试图重新调整removeFirst方法的用途,但我搞砸了,尝试了几次,现在我完全被难倒了。这里有一个方法。将数组转换为列表,从列表中获取最后一个元素,从列表中删除最后一个元素,然后将其转换回数组
var numbersList = values.ToList();
var last = numbersList.Last();
numbersList.Remove(last);
values = numbersList.ToArray();
return last;
Sybren回答的另一种选择:
int lastValue = values[values.Length - 1];
int[] newValues = new int[values.Length - 1];
Array.Copy(values, newValues, newValues.Length);
values = newValues;
return lastValue;
或
如果不想使用
Array
类的任何现有方法,还可以:
int lastValue = values[values.Length - 1];
int[] newValues = new int[values.Length - 1];
for (int i = 0; i < newValues.Length; i++)
{
newValues[i] = values[i];
}
values = newValues;
return lastValue;
int lastValue=values[values.Length-1];
int[]newValues=newint[values.Length-1];
for(int i=0;i
编辑
算了吧,照@Steve说的去做。你只需要写就行了
public int removeLast()
{
if (isEmpty())
throw new Exception("List is Empty");
count--;
return values[count];
}
这将返回值数组中的最后一项,而不更改其大小,但会减少跟踪实际插入数组中的项的变量。请注意,我没有尝试更改count变量所指向位置中的值。它仍然存在,直到您添加另一个值覆盖它 所以你仍然可以写这个
// Creates an array with space for 10 ints
LinearArrayList la = new LinearArrayList();
la.addLast(34);
la.addLast(23);
la.addLast(56);
la.addLast(467);
la.addLast(43);
la.addLast(666);
la.addLast(7989);
la.addLast(82);
la.addLast(569);
la.addLast(100);
int value = la.removeLast();
// This will work because you still have a slot free in the 10 ints array
la.addLast(1110);
// While this will fail because the array has now all slots filled
la.addLast(9435);
Last=value[count]然后递减计数?您知道已经有一个数组类了……对吗?我主要学习c#和IDE(以及数据结构),我编写这个程序/类是为了更好地理解堆栈/数据管理,就像我说的,我会在库中使用预先编写的代码,但我发现它复杂且优化,我正在尝试复习基础知识。谢谢,最后的答案是我想要的,我真的不想使用现有的数组方法。这使不同的数组具有更小的大小。这意味着初始构造函数大小不再有效,因此它无法在上一个最后位置存储新值。若要完成,则需要一个add方法,该方法在大于当前长度时调整数组的大小。换句话说,您需要重新实现一个列表
// Creates an array with space for 10 ints
LinearArrayList la = new LinearArrayList();
la.addLast(34);
la.addLast(23);
la.addLast(56);
la.addLast(467);
la.addLast(43);
la.addLast(666);
la.addLast(7989);
la.addLast(82);
la.addLast(569);
la.addLast(100);
int value = la.removeLast();
// This will work because you still have a slot free in the 10 ints array
la.addLast(1110);
// While this will fail because the array has now all slots filled
la.addLast(9435);