Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有五个数字级别的分层大纲—;如何插入同级行或子行并调整现有记录?_C#_Oop_Document_Hierarchy_Outlining - Fatal编程技术网

C# 具有五个数字级别的分层大纲—;如何插入同级行或子行并调整现有记录?

C# 具有五个数字级别的分层大纲—;如何插入同级行或子行并调整现有记录?,c#,oop,document,hierarchy,outlining,C#,Oop,Document,Hierarchy,Outlining,我有一个带有“点”(或大纲)层次结构的表: 字段为L1 L3 L4 L5(L=级别) 例如: 1.0.0.0.0 1.1.0.0.0 1.1.1.0.0 1.1.2.0.0 1.2.0.0.0 如果我想在1.1.1.0.0插入一个同级,我应该得到一个新的1.1.2.0.0行-并且已经存在的1.1.2.0.0应该调整到1.1.3.0.0,以此类推 如果我想插入子级1.1.1.0.0,我应该得到一个新的1.1.1.1.0行,不需要进行调整,因为该级别上没有同级 我已经为此创建了过程代码——但它正在变

我有一个带有“点”(或大纲)层次结构的表: 字段为L1 L3 L4 L5(L=级别)

例如:

1.0.0.0.0
1.1.0.0.0
1.1.1.0.0
1.1.2.0.0
1.2.0.0.0

如果我想在1.1.1.0.0插入一个同级,我应该得到一个新的1.1.2.0.0行-并且已经存在的1.1.2.0.0应该调整到1.1.3.0.0,以此类推

如果我想插入子级1.1.1.0.0,我应该得到一个新的1.1.1.1.0行,不需要进行调整,因为该级别上没有同级

我已经为此创建了过程代码——但它正在变成意大利面条——我希望有一个OOP解决方案,其中包含一个处理这些插入和调整的类

有人能推荐哪怕是伪代码来处理这两种类型的插入和对已经存在的“行”所需的调整吗


任何帮助或建议都将不胜感激

给你评论的人我认为他们并不真正理解这个问题。您已经有了一个表,因此使用LinkedList只需一个表即可。您确实需要向该方法传递要插入的行和要插入的字段。仅仅添加一个值为1.1.1.0.0的新行并不能提供足够的信息来重新编号

下面的代码我使用了一个DataTable,每个列都有一个字段。为了简单地编写代码,我假设索引是整数。代码不是很复杂

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Outlining outling = new Outlining();
            outling.Add(2,2);
            outling.Add(0, 2);
            outling.Add(5, 2);
        }
    }
    public class Outlining
    {
        public DataTable dt = null;

        public Outlining()
        {
            dt = new DataTable();
            dt.Columns.Add("L1", typeof(int));
            dt.Columns.Add("L2", typeof(int));
            dt.Columns.Add("L3", typeof(int));
            dt.Columns.Add("L4", typeof(int));
            dt.Columns.Add("L5", typeof(int));

            dt.Rows.Add(new object[] { 1, 0, 0, 0, 0 });
            dt.Rows.Add(new object[] { 1, 1, 0, 0, 0 });
            dt.Rows.Add(new object[] { 1, 1, 1, 0, 0 });
            dt.Rows.Add(new object[] { 1, 2, 0, 0, 0 });
        }
        public void Add(int at, int level)
        {
            DataRow newRow = dt.Rows.Add();
            if (at < dt.Rows.Count - 1)
            {
                //move row if not last row
                dt.Rows.Remove(newRow);
                dt.Rows.InsertAt(newRow, at);
            }
            newRow.BeginEdit();
            newRow.ItemArray = dt.Rows[at + 1].ItemArray.Select(x => (object)x).ToArray();
            newRow.EndEdit();

            Renumber(at, level);
        }
        public void Renumber(int rowInsertIndex, int level)
        {
            for (int row = rowInsertIndex; row < dt.Rows.Count - 1; row++)
            {
                Boolean match = true;
                //check if columns to left still match, if no we are done
                for (int i = 0; i < level - 1; i++)
                {
                    if (dt.Rows[i][level] != dt.Rows[i + 1][level])
                    {
                        match = false;
                        break;
                    }
                }
                if (!match) break;
                dt.Rows[row + 1][level] = ((int)(dt.Rows[row + 1][level])) + 1;
            }
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统数据;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
大纲显示=新大纲显示();
概述。添加(2,2);
添加(0,2);
概述。添加(5,2);
}
}
公共课大纲
{
公共数据表dt=null;
公共大纲()
{
dt=新数据表();
添加(“L1”,类型(int));
添加(“L2”,类型(int));
添加(“L3”,类型(int));
添加(“L4”,类型(int));
添加(“L5”,类型(int));
Add(新对象[]{1,0,0,0,0});
Add(新对象[]{1,1,0,0,0});
Add(新对象[]{1,1,1,0,0});
Add(新对象[]{1,2,0,0,0});
}
公共void Add(int at,int level)
{
DataRow newRow=dt.Rows.Add();
如果(在(object)x.ToArray();
newRow.EndEdit();
重新编号(在、级别);
}
公共无效重新编号(整数行插入索引,整数级)
{
对于(int row=rowInsertIndex;row
每个对象都有自己的L1到L5字段。只需为同级添加链式列表实现,例如:
Item1.1
引用了
Item1.2
,并且为父级保留了对第一个子级的引用。然后,当添加一个元素时,你可以像
一样遍历每个级别的结构,而Lx不是y
,当达到最低级别时,像调整任何链表一样调整同级。谢谢Mat-但我不熟悉“链表实现”的概念-你能提供一个简单的C#示例吗?应该让你去做一个简单的理解。对于真正的实现,您只需使用
LinkedList
类并调用现有方法
AddAfter()
。再次感谢,Mat-甚至不知道LinkedList存在!让我深入研究一下这如何应用到我的场景中……你真的应该给出一些你的代码,关于这个问题有太多的东西可以假设。