Hard time adding array in list

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

48 Replies - 1232 Views - Last Post: 11 September 2018 - 02:44 PM Rate Topic: -----

#1 daygamer   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 110
  • Joined: 21-August 18

Hard time adding array in list

Posted 06 September 2018 - 12:00 PM

Hi,


            List<string[]> Lista = new List<string[]>();
            string[] array = new string[2] { ""  ,  "" };
            Lista.Add(array);


I just realized i have hard time adding later this

Lista.Add(array[0])
Lista.Add(array[1])



I am used to ordinary lists and this gives me hard time, why will it not just work? What am i suppoed doing?

everytime i run the program the new typed arrays in the list do not get saved together with the previous ones, what is the reason for that?

foreach (var item in Lista)
                        {
                            
                            Console.WriteLine(item);
                        }


This post has been edited by daygamer: 06 September 2018 - 12:06 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Hard time adding array in list

#2 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 888
  • View blog
  • Posts: 3,587
  • Joined: 13-June 14

Re: Hard time adding array in list

Posted 06 September 2018 - 12:05 PM

View Postdaygamer, on 06 September 2018 - 08:00 PM, said:

I just realized i have hard time adding later this

Lista.Add(array[0])
Lista.Add(array[1])



Given the code you showed above, this doesn't really make sense, does it? Lista is a list whose elements are of type string[], given the declaration on line 1 of the first snippet of code you posted. If array here is the same as array on line 2 of your first snippet, then array[0], for example is of type string. A string is not the same as a string[] is it?

What exactly are you trying to do?
Was This Post Helpful? 0
  • +
  • -

#3 daygamer   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 110
  • Joined: 21-August 18

Re: Hard time adding array in list

Posted 06 September 2018 - 12:11 PM

I am adding new arrays in the list in a bool while state also i am changing the previous arrays in the list by deleting the list and adding new on the same map

This post has been edited by daygamer: 06 September 2018 - 12:12 PM

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14352
  • View blog
  • Posts: 57,554
  • Joined: 12-June 08

Re: Hard time adding array in list

Posted 06 September 2018 - 12:39 PM

Quote

everytime i run the program the new typed arrays in the list do not get saved together with the previous ones, what is the reason for that?

Where is the code for this?>
Was This Post Helpful? 0
  • +
  • -

#5 daygamer   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 110
  • Joined: 21-August 18

Re: Hard time adding array in list

Posted 06 September 2018 - 12:42 PM

Here is the code:

List<string[]> Lista = new List<string[]>();
            string[] array = new string[2] { ""  ,  "" };
            Lista.Add(array);
            bool isRunning = true;
           
            while (isRunning)
            {
                Rubrik();
                switch (Console.ReadKey(true).KeyChar)
                {
                    case '1':
                        Console.WriteLine("Skriv titel: ");
                        array[0] = Console.ReadLine();
                        Console.WriteLine("Skriv meddelande:");
                        array[1] = Console.ReadLine();
                        
                        break;

                    case '2':
                   
                        foreach(var item in Lista)
                        {
                            Console.WriteLine(item[0]);
                            Console.WriteLine(item[1]);
                        }
                        break;


This post has been edited by daygamer: 06 September 2018 - 12:45 PM

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6404
  • View blog
  • Posts: 21,972
  • Joined: 05-May 12

Re: Hard time adding array in list

Posted 06 September 2018 - 12:49 PM

The reason for that is because you only add one instance of array into your list on line 3. So even if you loop multiple times in your while(isRunning) loop and take in new values for the title and message, you are storing them into that single instance. If you want multiple diary entries, then you'll need to create multiple instances of the array which are disjoint from each other.
Was This Post Helpful? 0
  • +
  • -

#7 daygamer   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 110
  • Joined: 21-August 18

Re: Hard time adding array in list

Posted 07 September 2018 - 12:39 AM

How do i do that?
Was This Post Helpful? 0
  • +
  • -

#8 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 888
  • View blog
  • Posts: 3,587
  • Joined: 13-June 14

Re: Hard time adding array in list

Posted 07 September 2018 - 01:28 AM

You should also have a class to represent an entry, rather than using an array. Otherwise, you have to be aware of the significance of the array indices (what do 0 and 1 mean?) and writing string[] isn't as readable as writing Entry.

This post has been edited by ndc85430: 07 September 2018 - 01:29 AM

Was This Post Helpful? 2
  • +
  • -

#9 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2639
  • View blog
  • Posts: 4,210
  • Joined: 21-June 11

Re: Hard time adding array in list

Posted 07 September 2018 - 01:30 AM

Using new again and then adding the result to the list with Add. For example this:

List<string[]> arrays;
string[] array = new string[] {"hello", "world"};
arrays.Add(array);
array = new string[] {"goodbye", "world"};
arrays.Add(array);



Will leave you with the list {{"hello", "world"}, {"goodbye", "world"}}. As will this version that forgoes the array variable:

List<string[]> arrays;
arrays.Add(new string[] {"hello", "world"});
arrays.Add(new string[] {"goodbye", "world"});



Whereas this one:

List<string[]> arrays;
string[] array = new string[] {"hello", "world"};
arrays.Add(array);
array[0] = "goodbye";
arrays.Add(array);



Will give you the list {{"goodbye", "world"}, {"goodbye", "world"}} because you've only created one array and put two references to that same array into the list.

All of these version give you a list with two elements because Add is called twice. If Add is called only once, you only get one element.
Was This Post Helpful? 0
  • +
  • -

#10 daygamer   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 110
  • Joined: 21-August 18

Re: Hard time adding array in list

Posted 07 September 2018 - 02:03 AM

i made this so far:


List<string> ListaX = new List<string>();
            string[] array = new string[2] { "", "" };
            bool isRunning = true;        
            while (isRunning)

            {
                Rubrik();
                switch (Console.ReadKey(true).KeyChar)
                {
                    case '1':
                        Console.WriteLine("titel: ");
                        array[0] = Console.ReadLine();
                        Console.WriteLine("message:");
                        array[1] = Console.ReadLine();
                        ListaX.Add(array[0]);
                        ListaX.Add(array[1]);

                        break;



in case 3 where i am supposed to find the files i have written this

case '3':
                        try
                        {
                            Console.WriteLine("search: ");
                            Console.WriteLine("titel: ");
                            string search = Console.ReadLine();
                          

                           
                            for (int i = 0; i < ListaX.Count; i++)
                            {
                                if (search == ListaX[i])
                                {
                                        Console.WriteLine("Found:");
                                        Console.WriteLine("title :" + ListaX[i]);
                                        Console.WriteLine("message: " + array[i + 1]);
                                        
                                }
                            }
                        }
                        catch
                        {
                            Console.WriteLine("fault");
                        }
                        break;



This works fine untill i save 3 or more files in the list. The message
Console.WriteLine("message: " + array[i + 1]);
causes error code

how can i write in line that ''<-- this message belongs to ---> '' this file?

the title is working to show which fine it belongs to

This post has been edited by daygamer: 07 September 2018 - 02:12 AM

Was This Post Helpful? 0
  • +
  • -

#11 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 888
  • View blog
  • Posts: 3,587
  • Joined: 13-June 14

Re: Hard time adding array in list

Posted 07 September 2018 - 02:15 AM

It would really help us if you told us what the error was. What do you think the problem is, or what have you done to find that out?
Was This Post Helpful? 0
  • +
  • -

#12 daygamer   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 110
  • Joined: 21-August 18

Re: Hard time adding array in list

Posted 07 September 2018 - 02:51 AM

I am trying to figure out what the algorithm should be to get the outprint in correct order when searching on title

Title: with 'a'
Message: with 'b'
This shows when searching on 'a'.


when searching on 'b' getting catch

when searching on 'c' getting 'd'

You see
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6404
  • View blog
  • Posts: 21,972
  • Joined: 05-May 12

Re: Hard time adding array in list

Posted 07 September 2018 - 07:11 AM

View Postdaygamer, on 07 September 2018 - 05:51 AM, said:

when searching on 'b' getting catch

Did you mean getting an exception? If so, what exception are you getting?

Did you enter data in the order 'a', 'b', 'c', 'd'? If so, based on your latest code above, you should have only gotten an exception when searching for 'd'.
Was This Post Helpful? 0
  • +
  • -

#14 daygamer   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 110
  • Joined: 21-August 18

Re: Hard time adding array in list

Posted 07 September 2018 - 07:33 AM

that was a great wake-up call for me
Was This Post Helpful? 0
  • +
  • -

#15 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6404
  • View blog
  • Posts: 21,972
  • Joined: 05-May 12

Re: Hard time adding array in list

Posted 07 September 2018 - 08:12 AM

Argh! I misread line 16. You are correct. You would have gotten an exception while searching for 'b'. But you should also have gotten exceptions when searching for 'c' and 'd'. That is because your line 16 looks like this:
Console.WriteLine("message: " + array[i + 1]);


When 'b' is found in your list, i is equal to 1. 1+1 ==2. array[2] will be out of bounds since your array is only 2 elements big and C# arrays are zero based. Similarly, when i is equal to 2 or 3 to match 'c' or 'd', then you would again be out of bounds.
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »