Array Util (C#)

本文介绍了一组用于C#编程语言中的二维数组实用函数,包括矩阵乘法(ArrayMultiply)、初始化(ArrayInitialization)和展示(ArrayPresentation)等功能。通过这些函数可以更方便地进行科学计算和数据分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Here collects the C# implementation of Array utility functions for group research.

Array Multiply

Parameters: 2-d array * 2-d array

Return: 2-d array

		*Multiply 2-d arrays 
		*Param
		*/
		public static double[,] ArrayMul(double[,] a, double[,] b){
			if(a.GetLength(1)!=b.GetLength(0)){
				WriteLine("Row number in a is NOT equal to column number in b!");
				return null;
			}
			double[,] result = new double[a.GetLength(0),b.GetLength(1)];
			for(int i=0;i<a.GetLength(0);i++) {
				for(int j=0;j<b.GetLength(1);j++){
					double c = 0;
					for(int k=0;k<a.GetLength(1);k++){
						c+=a[i,k]*b[k,j];
					}
					result[i,j]=c;
				}
			}
			return result;
		}

Attention:

1. To use GetLength() for the length of different dimension.

2. To use [,] for 2-d array definition.

Array Initialization

Parameters: 2-d array

Return: void

		/***
		*Initialize 2-d array with random doubles (with given max)
		*/
		public static void ArrayInit(ref double[,] a, double max){
			Random rd = new Random();
			for(int i=0; i<a.GetLength(0); i++){
				for(int j=0; j<a.GetLength(1); j++){
					a[i,j] = rd.Next((int)max)+rd.NextDouble();
				}
			}
		}

Attention:

1. Random is defined under the namespace System, so please add `using System;'.

2. NextDouble() returns a double value (0,1), so if you need the integer part of a random value, refer to Next().

Array Presentation

Parameters: 2-d array

Return: void

		/***
		*Print out the entries in the given 2-d array.
		*/
		public static void ArrayPres(double[,] a){
			for(int i=0;i<a.GetLength(0);i++){
            	for(int j = 0; j<a.GetLength(1); j++){
            		Write($"{a[i,j]} ");
            	}
            	WriteLine();
            }
		}


### 关于 `toStringArray` 方法 在C#和Java编程语言中,并不存在名为 `toStringArray` 的内置标准库方法。然而,可以根据具体需求实现自定义的 `toStringArray` 方法。 #### 自定义 `toStringArray` 方法 对于字符串处理的需求,通常可以通过编写辅助函数来满足特定的应用场景。下面展示如何在一个类中实现该功能: ##### C# 为了将对象转换成字符串数组,在C#中可以创建一个扩展方法或者静态工具方法来进行此操作。这里给出一种简单的实现方案[^1]: ```csharp public static class ArrayExtensions { public static string[] ToStringArray(this object obj) { if (obj is IEnumerable enumerable && !(obj is string)) { var list = new List<string>(); foreach (var item in enumerable) list.Add(item.ToString()); return list.ToArray(); } throw new ArgumentException("Parameter must be an enumerable type."); } } ``` 上述代码片段展示了如何利用泛型集合遍历输入参数并将其元素逐一转化为字符串形式存入列表中最后返回作为结果集。 ##### Java 而在Java里,则可能更倾向于直接针对某种类型的容器(比如List<String>)设计这样的帮助器方法[^2]: ```java import java.util.List; import java.util.Arrays; public class Util { public static String[] toStringArray(List<?> list){ return list.stream().map(Object::toString).toArray(String[]::new); } } ``` 这段程序使用了流式API简化了从任意类型列表到字符串数组之间的映射过程。 #### 注意事项 当尝试构建自己的 `toStringArray` 函数时需要注意以下几点: - 输入数据结构是否支持迭代; - 对null值和其他特殊情况做适当处理; - 考虑性能影响特别是面对大数据量的时候;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值