添加Lesson1,ArrayList

This commit is contained in:
2025-10-10 09:39:57 +08:00
parent 341fa3a006
commit 114536061f
6 changed files with 227 additions and 1 deletions
+13 -1
View File
@@ -1,10 +1,14 @@
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.14.36202.13 d17.14 VisualStudioVersion = 17.14.36202.13
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test", "test\test.csproj", "{5B032C55-6E02-4EA9-BE52-083A6DA91295}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test", "test\test.csproj", "{5B032C55-6E02-4EA9-BE52-083A6DA91295}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson1_ArrayList", "Lesson1_ArrayList\Lesson1_ArrayList.csproj", "{A3FB97E5-BFB8-41F5-AA91-79C16EC75513}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson1_ArrayList练习题", "Lesson1_ArrayList练习题\Lesson1_ArrayList练习题.csproj", "{B9F9E174-21EA-40E7-A139-58FFD3448023}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@@ -15,6 +19,14 @@ Global
{5B032C55-6E02-4EA9-BE52-083A6DA91295}.Debug|Any CPU.Build.0 = Debug|Any CPU {5B032C55-6E02-4EA9-BE52-083A6DA91295}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5B032C55-6E02-4EA9-BE52-083A6DA91295}.Release|Any CPU.ActiveCfg = Release|Any CPU {5B032C55-6E02-4EA9-BE52-083A6DA91295}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5B032C55-6E02-4EA9-BE52-083A6DA91295}.Release|Any CPU.Build.0 = Release|Any CPU {5B032C55-6E02-4EA9-BE52-083A6DA91295}.Release|Any CPU.Build.0 = Release|Any CPU
{A3FB97E5-BFB8-41F5-AA91-79C16EC75513}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A3FB97E5-BFB8-41F5-AA91-79C16EC75513}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A3FB97E5-BFB8-41F5-AA91-79C16EC75513}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A3FB97E5-BFB8-41F5-AA91-79C16EC75513}.Release|Any CPU.Build.0 = Release|Any CPU
{B9F9E174-21EA-40E7-A139-58FFD3448023}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B9F9E174-21EA-40E7-A139-58FFD3448023}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B9F9E174-21EA-40E7-A139-58FFD3448023}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B9F9E174-21EA-40E7-A139-58FFD3448023}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
+86
View File
@@ -0,0 +1,86 @@
using System.Collections;
namespace Lesson1_ArrayList
{
internal class Program
{
static void Main(string[] args)
{
#region
//C#核心中 索引器的练习题
//自定义一个整形数组类,该类中有一个整形数组变量
//为他封装增删查改的方法
#endregion
#region ArrayList的本质
//ArrayList是c#为我们封装好的类
//本质是一个Object类型的数组
//ArrayList可以帮我们实现增删改查等功能
#endregion
#region
//注意:使用ArrayList需要引入命名空间 using System.Collections;
ArrayList arrayList = new ArrayList();
#endregion
///可以用F12进ArrayList中查看方法或去微软官方文档查看
#region
//增
arrayList.Add(1.2f);
arrayList.Add("123");
arrayList.Add(new Object());
arrayList.Add(true);
//插入
arrayList.Insert(0, "插入的元素");//在指定索引位置插入元素,原来该位置及其后面的元素依次后移
arrayList.AddRange(new int[] { 1, 2, 3, 4, 5 });//一次性添加多个元素
//删
arrayList.Remove("123");//移除指定元素,如果有多个相同元素,只移除第一个
arrayList.RemoveAt(0);//移除指定索引位置的元素
arrayList.RemoveRange(0, 2);//移除从指定索引位置开始的指定个数的元素
arrayList.Clear();//清空所有元素
//改
arrayList[1] = 4;
//查
Console.WriteLine(arrayList[0]);
if (arrayList.Contains("123"))
{
//如果包含指定元素执行的代码
}
//正向查找指定的元素,返回索引位置,如果没有找到返回-1
int index = arrayList.IndexOf(3);
Console.WriteLine(index);
//反向查找指定的元素,返回索引位置,如果没有找到返回-1
int lastIndex = arrayList.LastIndexOf(3);
Console.WriteLine(lastIndex);
#region
Console.WriteLine(arrayList.Count);//获取元素个数(长度)
Console.WriteLine(arrayList.Capacity);//获取ArrayList的容量(用于以后避免产生过多垃圾)
for (int i = 0; i < arrayList.Count; i++)
{
Console.WriteLine(arrayList[i]);
}
//迭代器遍历
foreach (var item in arrayList)//有个var类型临时变量item来接每次遍历到的元素
{
Console.WriteLine(item);
}
#endregion
#endregion
#region
//ArrayList存储的是Object类型的元素
//所以Object类型的数组存储的是值类型时会发生装箱拆箱
//装箱:值类型转换为引用类型的过程
//拆箱:引用类型转换为值类型的过程
//所以ArrayList存储值类型时会影响性能尽量少用
int i = 1;
arrayList.Add(i);//发生装箱
int j = (int)arrayList[0];//发生拆箱
#endregion
}
}
}
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
@@ -0,0 +1,85 @@
using System.Collections;
namespace Lesson1_ArrayList练习题
{
class Backpack
{
ArrayList stuff;
int money;
public Backpack()
{
money = 0;
stuff = new ArrayList();
}
public Backpack(int money)
{
stuff = new ArrayList();
this.money = money;
}
public void ShowBackPack()
{
Console.WriteLine("背包物品有:");
for (int i = 0; i < stuff.Count; i++)
{
if (i + 1 != stuff.Count)
{
Console.Write(stuff[i] + "、");
}
else
{
Console.WriteLine(stuff[i]);
}
}
}
public void BuyStuff(Object obj,int cost)
{
if(cost > money)
{
Console.WriteLine("余额不足");
}
else
{
stuff.Add(obj);
money -= cost;
Console.WriteLine("成功购买物品:" + obj);
}
}
public void SoldStuff(object obj,int earned)
{
if (stuff.Contains(obj))
{
stuff.Remove(obj);
money += earned;
Console.WriteLine("成功出售物品:" + obj);
}
else
{
Console.WriteLine("没有该物品,无法出售:" + obj);
}
}
public void ShowMoney()
{
Console.WriteLine("当前余额:" + money);
}
}
class Program
{
static void Main(string[] args)
{
Backpack b1= new Backpack();
Backpack b2 = new Backpack(100);
b1.ShowBackPack();
b2.ShowBackPack();
b1.BuyStuff("sth1", 50);
b2.BuyStuff("sth2", 50);
b1.ShowBackPack();
b2.ShowBackPack();
b1.SoldStuff("sth1", 25);
b2.SoldStuff("sth2", 25);
b1.ShowBackPack();
b2.ShowBackPack();
b1.ShowMoney();
b2.ShowMoney();
}
}
}
+23
View File
@@ -6,5 +6,28 @@
{ {
Console.WriteLine("Hello, World!"); Console.WriteLine("Hello, World!");
} }
#region
#endregion
#region
#endregion
#region
#endregion
#region
#endregion
#region
#endregion
#region
#endregion
#region
#endregion
#region
#endregion
} }
} }