-2

I am working in the program bellow, I aim to store data variable array using List.

int[] data = new int[] { 65, 66, 67, 32, 100, 90 };  // I declare int[] data it contain my data that I want to work with the length change.
int[] array = new int[6 * data.Length];              // I declare a table length of 6 * the lenght of the data


List<int> list = new List<int>();                    // I declared a list where I wand to Add to build array 

foreach (var b in data)   
       {
        array[0] = b / 200;
        array[1] = b - array[0] / 79;
        array[2] = b - array[0] - array[1] / 27;
        array[3] = b - array[0] - array[1] - array[2] / 19;
        array[4] = b - array[0] - array[1] - array[2] - array[3] / 21;
        array[5] = b - array[0] - array[1] - array[2] - array[3] - array[4] / 3;

//   list.Add();  // in this line I need to write an instruction where I can list.Add(array)
            }

            Console.WriteLine("", array); 
Console.ReadKey();
        }

I really appreciate any help.

kind regards

  • 5
    What do you expect `list.Add(array)` to do? Knowing the final result you expect will help us tell you how to change the code, but the compiler is right. You can't add an int array to a list that stores ints. – Lasse V. Karlsen Nov 13 '20 at 13:13
  • You want to create a list of arrays? Or what? It's really unclear what the result should be, or why. To be honest the whole thing doesn't make a lot of sense...why would you store an array in a list? Maybe you need a multi-dimensional array. Or do you just want to put all the items in `array` into `list`, separately? Please clarify. – ADyson Nov 13 '20 at 13:13
  • `list.AddRange(array)` would add each item in the array to the list. – Johnathan Barclay Nov 13 '20 at 13:13
  • @ADyson yes I need to create a list of array. My purpose is to build array [6 * data.lenght]. – Ahmed Amin Nov 13 '20 at 13:16
  • `var list = new List()`; – Johnathan Barclay Nov 13 '20 at 13:17
  • thank you for your reply @JohnathanBarclay, I compiled to code after your comment but unfortunately it not working. I didn't get any output in my console. – Ahmed Amin Nov 13 '20 at 13:19
  • @JohnathanBarclay I added `var list = new List()` and in `list.AddRange(array);` array is error cannot convert from int[] to system.collection.generic.IEnumerable – Ahmed Amin Nov 13 '20 at 13:22
  • You are not getting output because `Console.WriteLine("", array)` is wrong. The empty string is a format string and array its parameter, so you are just writing the empty string. – Palle Due Nov 13 '20 at 13:23
  • @LasseV.Karlsen thank you for your reply. I need to add an int array to a list that stores ints. could you please help with it – Ahmed Amin Nov 13 '20 at 13:24
  • `list.AddRange(array);` assumed you wanted to add each integer to the list. If you actually want a `List`, then you need to stick with `list.Add(array);` – Johnathan Barclay Nov 13 '20 at 13:24
  • Why are you declaring an int[36] and then only using the first 6 indices? – Palle Due Nov 13 '20 at 13:25
  • "I need to create a list of array."...ok but **why**? That's a very odd requirement really. What about a multi-dimensional array? Or a more sophisticated data structure than an an array? What is the actual purpose of all this code? – ADyson Nov 13 '20 at 13:27
  • @ADyson I need to build an array for an input data and do some operation on it to build the other data that I want to use it after in my program. I am stuck on this problem so could you please help to sort it out. – Ahmed Amin Nov 13 '20 at 13:31
  • @PalleDue may I know what I have to write, please. I tried this but it not working `int[] result = array.ToArray(); Console.WriteLine("", result); ` – Ahmed Amin Nov 13 '20 at 13:33
  • `array.ToArray();` makes no sense - `array` is already an array, so this method achieves nothing. Remember that to write to the Console, which shows text, then the thing you are writing needs to be a _text_ value - i.e. a string (or something like a number which is easy to convert to a string). Arrays can't be converted to strings easily - there's no way to define how it should appear in text form. If you want to show all the values in an integer array, then loop through the array and write each individual value to the console. – ADyson Nov 13 '20 at 13:37
  • _"I need to build an array for an input data and do some operation on it to build the other data that I want to use it after in my program"_ ...this is so vague as an explanation as to be completely meaningless. It could define almost any data processing operation ever created. It doesn't explain _your_ situation. – ADyson Nov 13 '20 at 13:37
  • @ADyson in the result of my program I need to get something like this `30 65 89 78 56 78 34 56 78 90 23 34 45 56 67 78 43 45....` int[] array [] and the length is 6* initial data.lengh – Ahmed Amin Nov 13 '20 at 13:38
  • So do as I suggested then and loop through the values and write each one individually to the console. But TBH shouldn't it be your _list_ you are looping through, since that's what you're trying to create - I assume the purpose of the console logging is that you want to test the contents of it? – ADyson Nov 13 '20 at 13:39
  • P.S. if you find yourself trying to do something relatively standard, like write an array to the console, and you are failing, then just type something like "C# write array to console" into google and you get 100 suggestions already. No need to ask us really! But here's https://stackoverflow.com/questions/16265247/printing-all-contents-of-array-in-c-sharp, to save you the terrible effort of doing some basic research of your own. – ADyson Nov 13 '20 at 13:41
  • @ADyson I am sorry, I am trying to just to sort this problem out, I tried to do what other people suggest but my problem didn't be fixed – Ahmed Amin Nov 13 '20 at 14:00
  • That's because people showed you ways to deal with the data structure (which were largely valid suggestions). Displaying it on the screen is a separate task. – ADyson Nov 13 '20 at 15:04

2 Answers2

1

You want to add all the items in the array to the list?

list.AddRange(array);

If you are trying to add something else like the sum you can use System.Linq for that

list.Add(array.Sum(r => r));
Ready Cent
  • 1,821
  • 3
  • 18
  • 25
1

List implements the Method AddRange with this you can add an Array/List to it.

int[] data = new int[] { 65, 66, 67, 32, 100, 90 };
List<int> list = new List<int>();
list.AddRange(data);

Source: https://learn.microsoft.com/de-de/dotnet/api/system.collections.generic.list-1.addrange?view=net-5.0

Dharman
  • 30,962
  • 25
  • 85
  • 135
Tobias Münch
  • 829
  • 4
  • 13