比较C#中的数组元素

本文关键字:数组元素 比较 | 更新日期: 2023-09-27 18:00:37

两个数组,即itemtra

item包含

B
C
M
G
D
E

tra阵列包含

BM
BDGE
MDGC
BMDG
BMDC

我需要找到tra阵列中存在的item阵列中每个元素的计数

例如B计数为4 C计数为2

对于这个任务,我使用了count()和计数器变量。

这是我的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace apriori
{
    class apriori
    {
        static void Main(string[] args)
        {
            int t, n, s, i, j, k, q;
            int counter = 0;
            int[] count = new int[6];
            Console.WriteLine("enter the number of transactions t");
            t = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("enter the number of items n");
            n = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("enter minimum support s");
            s = Convert.ToInt32(Console.ReadLine());
            String[] tra = new String[t];
            String[] item = new String[n];
            Console.WriteLine("enter transactions representing one alphabet each item");
            for (i = 0; i < t; i++)
            {
                tra[i] = Console.ReadLine();
            }
            Console.WriteLine("list of transactions");
            foreach (String it in tra)
            {
                Console.WriteLine(it);
            }
            Console.WriteLine("enter items");
            for (j = 0; j < n; j++)
            {
                item[j] = Console.ReadLine();
            }
            Console.WriteLine("list of items");
            foreach (String ite in item)
            {
                Console.WriteLine(ite);
            }
            for (i = 0; i < n; i++)
            {
               for (j = 0; j < t; j++)
                {
                    if (item[i]==tra[j])
                    {
                      count[i]=counter++;
                    }
                } 
                counter=0;
            }

比较C#中的数组元素

您的程序不适用于n>6,如:

int[] count = new int[6];
 for (i = 0; i < n; i++)
 {
      for (j = 0; j < t; j++)
      {
           if (item[i]==tra[j])
           {
                // this index is out of bounds when i>=6
                count[i]=counter++;
           }
      } 
      counter=0;
 }