-1

在for循环中我做了:

string[] xFrames = new string[wocl.Count];
            string[] yFrames = new string[wocl.Count];
            List<float> Xframes = new List<float>(wocl.Count);
            List<float> Yframes = new List<float>(wocl.Count);
            for (int i = 0; i < wocl.Count; i++)
            {
                string X = xFrames[i] = string.Format("Frame_X_{0} ", i + 1);
                string Y = yFrames[i] = string.Format("Frame_Y_{0} ", i + 1);

                for (int j = 0; j < wocl[i].Point_X.Count; j++)
                {
                   // xFrames[i] += string.Format("{0},", wocl[i].Point_X[j]);
                   // yFrames[i] += string.Format("{0},", wocl[i].Point_Y[j]);
                    Xframes.Add(wocl[i].Point_X[j]);
                    Yframes.Add(wocl[i].Point_X[j]);

                }

                //xFrames[i].Trim(",".ToCharArray());
                //yFrames[i].Trim(",".ToCharArray());

                setting_file.SetListFloatKey(X, Xframes);
                setting_file.SetListFloatKey(Y, Yframes);

            }

文本文件中的结果是:

Frame_X_1 =325,332,322,332,325
Frame_Y_1 =325,332,322,332,325
Frame_X_2 =325,332,322,332,325,318,332,322,332,325
Frame_Y_2 =325,332,322,332,325,318,332,322,332,325
Frame_X_3 =325,332,322,332,325,318,332,322,332,325,318,332,322,355,325
Frame_Y_3 =325,332,322,332,325,318,332,322,332,325,318,332,322,355,325
Frame_X_4 =325,332,322,332,325,318,332,322,332,325,318,332,322,355,325,318,388,322,355,325
Frame_Y_4 =325,332,322,332,325,318,332,322,332,325,318,332,322,355,325,318,388,322,355,325

不知道为什么 = 之后右边的数字应该在每帧 x 和 y 相同的数字/坐标中。

像在 Frame_X_1 而不是在 Frame_X_4 或不像在 Frame_X_2 出于某种原因,它不断添加数字。

我使用的 setting_file 连接到 OptionsFile 类:

/*----------------------------------------------------------------
 * Module Name  : OptionsFile
 * Description  : Saves and retrievs application options
 * Author       : Danny
 * Date         : 10/02/2010
 * Revision     : 1.00
 * --------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Configuration;


/*
 *  Introduction :
 * 
 *  This module helps in saving application options
 * 
 * 
 *  Typical file could look like this:
 *  user_color=Red
 *  time_left=30
 *  
 * 
 * 
 * 
 * 
 * */

namespace DannyGeneral
{
    class OptionsFile
    {
        /*----------------------------------------
         *   P R I V A T E     V A R I A B L E S 
         * ---------------------------------------*/


        /*---------------------------------
         *   P U B L I C   M E T H O D S 
         * -------------------------------*/
        string path_exe;
        string temp_settings_file;
        string temp_settings_dir;
        string Options_File;
        StreamWriter sw;
        StreamReader sr;

/*----------------------------------------------------------
 * Function     : OptionsFile
 * Description  : Constructor
 * Parameters   : file_name is the name of the file to use
 * Return       : none
 * --------------------------------------------------------*/
    public OptionsFile(string settings)
    {
        if (!File.Exists(settings))
        {
            if (!Directory.Exists(Path.GetDirectoryName(settings)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(settings));
            }
            File.Create(settings).Close();
        }
        path_exe = Path.GetDirectoryName(Application.LocalUserAppDataPath);
        Options_File = settings; 
    }

/*----------------------------------------------------------
 * Function     : GetKey
 * Description  : gets the value of the key.
 * Parameters   : key
 * Return       : value of the key if key exist, null if not exist
 * --------------------------------------------------------*/
    public string GetKey(string key)
    {

      //  string value_of_each_key;
        string key_of_each_line;
        string line;
        int index;
        string key_value;
        key_value = null;

        sr = new StreamReader(Options_File);
        while (null != (line = sr.ReadLine()))
        {


            index = line.IndexOf("=");


           //    value_of_each_key = line.Substring(index+1);



            if (index >= 1)
            {
                key_of_each_line = line.Substring(0, index);
                if (key_of_each_line == key)
                {
                    key_value = line.Substring(key.Length + 1);
                }

            }
            else
            {
            }


        }
        sr.Close();
        return key_value;
    }


/*----------------------------------------------------------
 * Function     : SetKey
 * Description  : sets a value to the specified key
 * Parameters   : key and a value
 * Return       : none
 * --------------------------------------------------------*/
    public void SetKey(string key , string value)
    {
        bool key_was_found_inside_the_loop;
        string value_of_each_key;
        string key_of_each_line ;
        string line;
        int index;
        key_was_found_inside_the_loop = false;

        temp_settings_file = "\\temp_settings_file.txt";
        temp_settings_dir = path_exe + @"\temp_settings";
        if (!Directory.Exists(temp_settings_dir))
        {
            Directory.CreateDirectory(temp_settings_dir);
        }

        sw = new StreamWriter(temp_settings_dir+temp_settings_file);
        sr = new StreamReader(Options_File);
        while (null != (line = sr.ReadLine()))
        {

            index = line.IndexOf("=");
            key_of_each_line = line.Substring(0, index);
            value_of_each_key = line.Substring( index + 1);
         //   key_value = line.Substring(0,value.Length);
            if (key_of_each_line == key)
            {
                sw.WriteLine(key + " = " + value);
                key_was_found_inside_the_loop = true;

            }
            else
            {
                sw.WriteLine(key_of_each_line+"="+value_of_each_key);
            }

        }

        if (!key_was_found_inside_the_loop)
        {
           sw.WriteLine(key + "=" + value);
        }
        sr.Close();
        sw.Close();
        File.Delete(Options_File);
        File.Move(temp_settings_dir + temp_settings_file, Options_File);
        return;

    }



    public List<float> GetListFloatKey(string keys)
    {
        List<float> result = new List<float>();
        string s = GetKey(keys);
        if (s != null)
        {
            string[] items = s.Split(new char[] { ',' });
            float f;
            foreach (string item in items)
            {
                if (float.TryParse(item, out f))
                    result.Add(f);
            }
            return result;
        }
        else
        {
            return result;
        }
    }


    public void SetListFloatKey(string key, List<float> Values)
    {
        StringBuilder sb = new StringBuilder();
        foreach (float value in Values)
        {
            sb.AppendFormat(",{0}", value);
        }
        if (Values.Count == 0)
        {
        }
        else
        {
            SetKey(key, sb.ToString().Substring(1));
        }
    }

不知道这里的数字有什么问题。

4

3 回答 3

0

您以这种方式使用双循环:

string[] xFrames = new string[wocl.Count];
string[] yFrames = new string[wocl.Count];

for (int i = 0; i < wocl.Count; i++)
{
   xFrames[i] = string.Format("Frame_X_{0} ", i + 1);
   yFrames[i] = string.Format("Frame_Y_{0} ", i + 1);

   for(int j =0; j < wocl[i].Length; j++)
   {
       xFrames[i] += string.Format("{0},", wocl[i].Point_X[j]);
       yFrames[i] += string.Format("{0},", wocl[i].Point_Y[j]);
   }

   xFrames[i].Trim(",".ToCharArray());
   yFrames[i].Trim(",".ToCharArray());
}
于 2012-07-16T20:21:06.397 回答
0

很难说出你想要做什么。但据我所知,您希望通过应用某种转换或组合来生成结果列表,将两个或更多列表组合成其他列表。

为此,我认为您可能需要查看Enumerable.ZipEnumerable.Zip<TFirst, TSecond, TResult>扩展方法。

例如,如果您有两个列表(伪代码):

val = 1, 2, 3, 4
names = "one", "two", "three", "four"

Zip 算法将允许您将这些组合成一个 int、string 元组的列表:

result = (1,"one"), (2,"two"), (3,"three"), (4,"four")

希望我在这里适合您尝试做的事情。

于 2012-07-16T20:21:22.910 回答
0

您似乎想要迭代迭代。

var output = new StringBuilder();
for(var index=0; index < wocl.Count; index++)
{
  var item = wocl[index];
  output.AppendFormat("Frame_X_{0}", index);

  for(var index2=0; index2 < item.Point_X.Count; index2++ )
  {
     output.AppendFormat(" {0}", item.Point_X[index2]);
  }

  output.AppendFormat("Frame_Y_{0}", index);

  for(var index2=0; index2 < item.Point_Y.Count; index2++ )
  {
     output.AppendFormat(" {0}", item.Point_Y[index2]);
  }
}

string resultText = output.ToString();
于 2012-07-16T20:23:05.103 回答