C# Remove unused index in an array with a declared size











up vote
-2
down vote

favorite












I have an assignment to make a method. The method requires to ask for a user input and store the user input in an Array, the Array initializes with sentinel values until the array is filled with user input.
The method will print all the value that user input.



So far, the program works except when the user does not fill all the array.
It means the array still use the initialized values(sentinel values), It will show a bunch of zeros to the unused index of the array.



For example:
int sample = new int[5]; // max size of array
// user input 3 numbers with loop (for ex. 1, 2, 3 and then exits with 555)



how do I make it display as



1
2
3



instead of



1
2
3
0
0










share|improve this question
























  • Must you use an array, rather than a List<>? You could have an array of nullable integers, new int?[5].
    – Jeppe Stig Nielsen
    Nov 12 at 21:46










  • You can either use a List or you can count the number of inputs and only display up to that count
    – Steve
    Nov 12 at 21:46










  • @JeppeStigNielsen i wouldnt suggest an absolute beginner to use nullable types just yet. there are way better ways to solve it.
    – Steve
    Nov 12 at 21:47






  • 1




    I believe homework questions are generally frowned upon, but the reason you get 1 2 3 0 0 is because you initialize an array of 5 int values. The default initialized value of an integer is 0 in C#. when you say display are you trying to display just the final value? if so you can loop through the array and build a result variable result += sample[i].ToString() and break out of the loop if you hit your sentinel value.
    – Andrei
    Nov 12 at 21:47










  • There are 50 different ways you could do it as you are already finding out. If you have to use an array and you don't have to specify a max size to begin with, you can use the Array.Resize method to change the size each time the user enters something valid....then you don't have to worry about erroneous data in the array at all.
    – user1011627
    Nov 12 at 21:50















up vote
-2
down vote

favorite












I have an assignment to make a method. The method requires to ask for a user input and store the user input in an Array, the Array initializes with sentinel values until the array is filled with user input.
The method will print all the value that user input.



So far, the program works except when the user does not fill all the array.
It means the array still use the initialized values(sentinel values), It will show a bunch of zeros to the unused index of the array.



For example:
int sample = new int[5]; // max size of array
// user input 3 numbers with loop (for ex. 1, 2, 3 and then exits with 555)



how do I make it display as



1
2
3



instead of



1
2
3
0
0










share|improve this question
























  • Must you use an array, rather than a List<>? You could have an array of nullable integers, new int?[5].
    – Jeppe Stig Nielsen
    Nov 12 at 21:46










  • You can either use a List or you can count the number of inputs and only display up to that count
    – Steve
    Nov 12 at 21:46










  • @JeppeStigNielsen i wouldnt suggest an absolute beginner to use nullable types just yet. there are way better ways to solve it.
    – Steve
    Nov 12 at 21:47






  • 1




    I believe homework questions are generally frowned upon, but the reason you get 1 2 3 0 0 is because you initialize an array of 5 int values. The default initialized value of an integer is 0 in C#. when you say display are you trying to display just the final value? if so you can loop through the array and build a result variable result += sample[i].ToString() and break out of the loop if you hit your sentinel value.
    – Andrei
    Nov 12 at 21:47










  • There are 50 different ways you could do it as you are already finding out. If you have to use an array and you don't have to specify a max size to begin with, you can use the Array.Resize method to change the size each time the user enters something valid....then you don't have to worry about erroneous data in the array at all.
    – user1011627
    Nov 12 at 21:50













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I have an assignment to make a method. The method requires to ask for a user input and store the user input in an Array, the Array initializes with sentinel values until the array is filled with user input.
The method will print all the value that user input.



So far, the program works except when the user does not fill all the array.
It means the array still use the initialized values(sentinel values), It will show a bunch of zeros to the unused index of the array.



For example:
int sample = new int[5]; // max size of array
// user input 3 numbers with loop (for ex. 1, 2, 3 and then exits with 555)



how do I make it display as



1
2
3



instead of



1
2
3
0
0










share|improve this question















I have an assignment to make a method. The method requires to ask for a user input and store the user input in an Array, the Array initializes with sentinel values until the array is filled with user input.
The method will print all the value that user input.



So far, the program works except when the user does not fill all the array.
It means the array still use the initialized values(sentinel values), It will show a bunch of zeros to the unused index of the array.



For example:
int sample = new int[5]; // max size of array
// user input 3 numbers with loop (for ex. 1, 2, 3 and then exits with 555)



how do I make it display as



1
2
3



instead of



1
2
3
0
0







c# arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 22:38









tonzter

233




233










asked Nov 12 at 21:40









otilolito

11




11












  • Must you use an array, rather than a List<>? You could have an array of nullable integers, new int?[5].
    – Jeppe Stig Nielsen
    Nov 12 at 21:46










  • You can either use a List or you can count the number of inputs and only display up to that count
    – Steve
    Nov 12 at 21:46










  • @JeppeStigNielsen i wouldnt suggest an absolute beginner to use nullable types just yet. there are way better ways to solve it.
    – Steve
    Nov 12 at 21:47






  • 1




    I believe homework questions are generally frowned upon, but the reason you get 1 2 3 0 0 is because you initialize an array of 5 int values. The default initialized value of an integer is 0 in C#. when you say display are you trying to display just the final value? if so you can loop through the array and build a result variable result += sample[i].ToString() and break out of the loop if you hit your sentinel value.
    – Andrei
    Nov 12 at 21:47










  • There are 50 different ways you could do it as you are already finding out. If you have to use an array and you don't have to specify a max size to begin with, you can use the Array.Resize method to change the size each time the user enters something valid....then you don't have to worry about erroneous data in the array at all.
    – user1011627
    Nov 12 at 21:50


















  • Must you use an array, rather than a List<>? You could have an array of nullable integers, new int?[5].
    – Jeppe Stig Nielsen
    Nov 12 at 21:46










  • You can either use a List or you can count the number of inputs and only display up to that count
    – Steve
    Nov 12 at 21:46










  • @JeppeStigNielsen i wouldnt suggest an absolute beginner to use nullable types just yet. there are way better ways to solve it.
    – Steve
    Nov 12 at 21:47






  • 1




    I believe homework questions are generally frowned upon, but the reason you get 1 2 3 0 0 is because you initialize an array of 5 int values. The default initialized value of an integer is 0 in C#. when you say display are you trying to display just the final value? if so you can loop through the array and build a result variable result += sample[i].ToString() and break out of the loop if you hit your sentinel value.
    – Andrei
    Nov 12 at 21:47










  • There are 50 different ways you could do it as you are already finding out. If you have to use an array and you don't have to specify a max size to begin with, you can use the Array.Resize method to change the size each time the user enters something valid....then you don't have to worry about erroneous data in the array at all.
    – user1011627
    Nov 12 at 21:50
















Must you use an array, rather than a List<>? You could have an array of nullable integers, new int?[5].
– Jeppe Stig Nielsen
Nov 12 at 21:46




Must you use an array, rather than a List<>? You could have an array of nullable integers, new int?[5].
– Jeppe Stig Nielsen
Nov 12 at 21:46












You can either use a List or you can count the number of inputs and only display up to that count
– Steve
Nov 12 at 21:46




You can either use a List or you can count the number of inputs and only display up to that count
– Steve
Nov 12 at 21:46












@JeppeStigNielsen i wouldnt suggest an absolute beginner to use nullable types just yet. there are way better ways to solve it.
– Steve
Nov 12 at 21:47




@JeppeStigNielsen i wouldnt suggest an absolute beginner to use nullable types just yet. there are way better ways to solve it.
– Steve
Nov 12 at 21:47




1




1




I believe homework questions are generally frowned upon, but the reason you get 1 2 3 0 0 is because you initialize an array of 5 int values. The default initialized value of an integer is 0 in C#. when you say display are you trying to display just the final value? if so you can loop through the array and build a result variable result += sample[i].ToString() and break out of the loop if you hit your sentinel value.
– Andrei
Nov 12 at 21:47




I believe homework questions are generally frowned upon, but the reason you get 1 2 3 0 0 is because you initialize an array of 5 int values. The default initialized value of an integer is 0 in C#. when you say display are you trying to display just the final value? if so you can loop through the array and build a result variable result += sample[i].ToString() and break out of the loop if you hit your sentinel value.
– Andrei
Nov 12 at 21:47












There are 50 different ways you could do it as you are already finding out. If you have to use an array and you don't have to specify a max size to begin with, you can use the Array.Resize method to change the size each time the user enters something valid....then you don't have to worry about erroneous data in the array at all.
– user1011627
Nov 12 at 21:50




There are 50 different ways you could do it as you are already finding out. If you have to use an array and you don't have to specify a max size to begin with, you can use the Array.Resize method to change the size each time the user enters something valid....then you don't have to worry about erroneous data in the array at all.
– user1011627
Nov 12 at 21:50












1 Answer
1






active

oldest

votes

















up vote
0
down vote













When you create a new array int sample = new int[5]; it will populate it with zeros if you don't define a default value to initialise it.



The other problem is that if you define an array of 5 elements and user continues to insert data, you won't be able to store that data.
You are better to work with List as you don't need an initial size and you can insert data into it without limite.



List<int> usersInts = new List<int>();

userInts.Add(//userInputsHere);





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270506%2fc-sharp-remove-unused-index-in-an-array-with-a-declared-size%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    When you create a new array int sample = new int[5]; it will populate it with zeros if you don't define a default value to initialise it.



    The other problem is that if you define an array of 5 elements and user continues to insert data, you won't be able to store that data.
    You are better to work with List as you don't need an initial size and you can insert data into it without limite.



    List<int> usersInts = new List<int>();

    userInts.Add(//userInputsHere);





    share|improve this answer

























      up vote
      0
      down vote













      When you create a new array int sample = new int[5]; it will populate it with zeros if you don't define a default value to initialise it.



      The other problem is that if you define an array of 5 elements and user continues to insert data, you won't be able to store that data.
      You are better to work with List as you don't need an initial size and you can insert data into it without limite.



      List<int> usersInts = new List<int>();

      userInts.Add(//userInputsHere);





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        When you create a new array int sample = new int[5]; it will populate it with zeros if you don't define a default value to initialise it.



        The other problem is that if you define an array of 5 elements and user continues to insert data, you won't be able to store that data.
        You are better to work with List as you don't need an initial size and you can insert data into it without limite.



        List<int> usersInts = new List<int>();

        userInts.Add(//userInputsHere);





        share|improve this answer












        When you create a new array int sample = new int[5]; it will populate it with zeros if you don't define a default value to initialise it.



        The other problem is that if you define an array of 5 elements and user continues to insert data, you won't be able to store that data.
        You are better to work with List as you don't need an initial size and you can insert data into it without limite.



        List<int> usersInts = new List<int>();

        userInts.Add(//userInputsHere);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 21:51









        YouneS

        1576




        1576






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270506%2fc-sharp-remove-unused-index-in-an-array-with-a-declared-size%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?