プログラミング素人のはてなブログ

プログラミングも電気回路も専門外の技術屋の末端が勉強したことや作品をアウトプットするブログ。コードに間違いなど見つけられたら、気軽にコメントください。 C#、Python3、ラズパイなど。

C# で配列の要素を並べ替える

要素の中身を最近使ったものから順番に並べ替える。

private void Form1_Load でファイルのなかみを読み込み、ComboBoxにInput。
CombBoxとはTextBoxに複数の要素をDropDownListから選べるもの。
f:id:s51517765:20170429112231j:plain

Listの中身はStreamReaderでよみこむ。
StreamReaderは必ず、srKeyword.Close() で開放しておく。
そうしないと、今度書き込むときにErrorがでる。

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) でアイテムが選択されたときに実行される。
要素の配列をひとつづつずらす。

            for (int n = 7 - 1; n >0 ; n--)
            {
                array_ex[n + 1] = array_ex[n]; //ひとつづつ配列のようそをずらして最新のものを[1]に入れる
              }

最後にファイルに書き込み記録。
ここでは、追記をfalseにして上書きする。
trueでは追記になって、実行されるたびに無駄に要素がふえてしまう。

StreamWriter sw = new StreamWriter("list.txt"<span style="color: #d32f2f">,false</span>, System.Text.Encoding.GetEncoding("shift_jis"))

本当は、重複要素のCheckとかしたい。


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;    //ファイルIO


namespace narabekae
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
         }

        string[] array_ex = new string[12];
        string[] array_temp = new string[12]; 

        private void Form1_Load(object sender, EventArgs e)
        {     //アプリ起動時に実行される
            int n = 1;
            var listKeyword = "";
            comboBox1.Items.Clear();  //コンボボックスの初期化
            StreamReader srKeyword = new StreamReader("list.txt");
            while ((listKeyword = srKeyword.ReadLine()) != null)
            {                                   
                if (listKeyword == null || listKeyword == "")  //要素の空欄はskip
                { }
                else { 
                    array_ex[n] = listKeyword;
                    comboBox1.Items.Add(listKeyword);
                    n++;
                }
              }
            srKeyword.Close(); //streamを閉じる
        }

        private void button1_Click(object sender, EventArgs e)
        {
               //要素を6つまで表示、必須ではない
                label1.Text = array_ex[1].ToString();
                label2.Text = array_ex[2].ToString();
                label3.Text = array_ex[3].ToString();
                label4.Text = array_ex[4].ToString();
                label5.Text = array_ex[5].ToString();
                label6.Text = array_ex[6].ToString();
            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {            //コンボボックスからItemが選択されたとき
            //値を取得
           string getItem = comboBox1.SelectedItem.ToString();
            label7.Text = getItem.ToString();
    
            for (int n = 7 - 1; n >0 ; n--)
            {
                array_ex[n + 1] = array_ex[n]; //ひとつづつ配列のようそをずらして最新のものを[1]に入れる
              }

            array_ex[1] = getItem;
            label1.Text = array_ex[1].ToString();
            label2.Text = array_ex[2].ToString();
            label3.Text = array_ex[3].ToString();
            label4.Text = array_ex[4].ToString();
            label5.Text = array_ex[5].ToString();
            label6.Text = array_ex[6].ToString();

            StreamWriter sw = new StreamWriter("list.txt",false, System.Text.Encoding.GetEncoding("shift_jis"));
            foreach (string eee in array_ex )
            {  //ファイルを更新
          
                sw.Write(eee + System.Environment.NewLine);
            }
            
            sw.Close(); //streamを閉じる
        }
     }
}