Trudności z zapisem do tablicy

0

Witam, rozpocząłem naukę C# i robię pewien projekcik. Mam taki problem że muszę wczytać plik który załączam (1.txt) Pierwszą linię muszę rozdzielić na dwie zmienne( liczba przedmiotów oraz pojemność) a kolejne linie to wartości które też muszę rozdzielić ( waga i wartość) i począwszy od drugiej linii chciałbym je wrzucić do tablicy aby móc później wykorzystać te dane w dalszej części programu. Niestety mam problem z wpisaniem ich do tablicy. Za wszelkie wskazówki/pomoc bardzo dziękuję!

 using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO; 
using System.Threading.Tasks;

namespace Problemplecakowy
{
    class Program
    {
        static void Main(string[] args)
        {
            int counter = 0;
            string line;
            string wybor;
            string[] word;
           // int[] tablica = new int[10];
            string[] dane;
          //  int i = 0;
            
            System.Console.WriteLine("Wybierz nr dokumentu w skali 1-10");
            wybor=System.Console.ReadLine();

            // Read the file and display it line by line.
            System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\Mateusz\Desktop\problem plecakowy\dane_testowe\"+ wybor + ".txt" );
            line = file.ReadLine(); 
            System.Console.WriteLine(line);
            
            
            // to split our string for quantity of elements and volume of backpack
            char[] delimiterChars = { ' ', ',', '.', ':', '\t' };
            string[] words = line.Split(delimiterChars);
            System.Console.WriteLine("{0} words in text:", words.Length);
            
            System.Console.WriteLine(" Liczba przedmiotow: " + words[0] + " Ładowność plecaka: " + words[1]);

            int n_przedmiotow = int.Parse(words[0]);
            int v_plecaka = int.Parse(words[1]);

            string[] t = File.ReadAllLines(@"C:\Users\Mateusz\Desktop\problem plecakowy\dane_testowe\" + wybor + ".txt");
          //  List<string> t = new List<string>();

            for (int i = 1; i <= n_przedmiotow; i++ )
            {
                
                // dzieli stringa na czesci
                char[] delimiterChar = { ' ', ',', '.', ':', '\t' };
                word = t[i].Split(delimiterChars);

                System.Console.WriteLine("Przedmiot: " +i + " Waga: " + word[0] + " Wartosc: " + word[1]);
                int waga = int.Parse(word[0]);
                int wartosc = int.Parse(word[1]);
//TUTAJ GENERALNIE ZACZYNA SIĘ MÓJ PROBLEM. Dane word[0] i word[1] chciałbym wrzucić do innej tablicy gdyż te są nadpisywane
// podczas działania pętli i wyświetlania ich w konsoli
                counter++;
               
                
            }
           // System.Console.WriteLine("A teraz niespodzianka" + word[0] + word[1]);  
                  
            System.Console.WriteLine("Rozbite liczby to: " + n_przedmiotow + " i " + v_plecaka);
            file.Close();
            
            System.Console.WriteLine("There were {0} lines.", counter);
            // Suspend the screen.
            System.Console.ReadLine();



        }
    }
}
2

Niezbyt rozumiem o co chodzi w pytaniu. Skoro chcesz je wrzucić do innej tablicy, wystarczy je tam zapisać :P.
np. deklarując int[] weights; przechowujące wagi i int[] values przechowujące wartości, oraz zapisując tam odpowiednie rzeczy.

Trochę przesadziłem z upraszczaniem kodu dla celu demonstracji, w każdym razie coś takiego:

static void Main(string[] args) {
    System.IO.StreamReader file = new System.IO.StreamReader(@"...");

    string[] header = file.ReadLine().Split();
    int itemCount = int.Parse(header[0]), capacity = int.Parse(header[1]);

    int[] weights = new int[itemCount], values = new int[itemCount];

    for (int i = 0; i < itemCount; i++) {
        string[] item = file.ReadLine().Split();
        weights[i] = int.Parse(item[0]);
        values[i] = int.Parse(item[1]);
    }

    Console.WriteLine("Wczytane przedmioty (ilość: {0}. pojemność: {1})", itemCount, capacity);
    for (int i = 0; i < itemCount; i++) {
        Console.WriteLine("{0}: waga={1}, wartość={2}", i, weights[i], values[i]);
    }

    Console.ReadKey();
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1