Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

c# - unhandled length exception halfway through program

I need to be able to print out the temperature array with user input and then print it out unsorted, and then copy that array from waterDepths at index 1 the next three values starting in array w at index 5 unsorted, sorted and reversed with the w array, but every time it runs I have two problems:

  1. the length exception but I don't understand why that happens
  2. before the exception it prints out the original array at index 0, and then it reprints for 0 and 1, and over and over until it's done it for the length of the array if that makes sense.

This is the error I run into Unhandled Exception: System.ArgumentException: length

using System;

class MainClass 
{
    static void Main () 
    {
        Console.Write("how many depths do you want to enter?: ");
        int depths = Convert.ToInt32(Console.ReadLine());
        int [] temperature = new int [depths];
        int [] w = new int [depths+5];

        InputValues(temperature);
        // array w will reference the same array
        // as the temperature array
        w = temperature;

        DisplayOutput(w, "waterDepth Array

");

        Array.Copy(temperature, 1, w, 5, 4);
        Console.WriteLine("Array w Not Sorted");
    
        Array.Sort(w); 
        DisplayOutput(w, "array w sorted 

");

        Array.Reverse(w);
        DisplayOutput(w, "array w reversed 

");
    } // end Main

    public static void InputValues(int [] temp)
    {
        string inValue;
        for(int i = 0; i< temp.Length; i++)
        {
            Console.Write("enter Temperature {0}: ", i+1);
            inValue = (Console.ReadLine());
            temp[i] = Convert.ToInt32(inValue);
        } // end for
    } // end input

    public static void DisplayOutput(int [] anArray,string msg)
    {
        foreach(double wVal in anArray)
        {
            if (wVal>0)
                msg +=wVal + "
";
        } // end foreach
    } //end disp
}//end class
question from:https://stackoverflow.com/questions/66059366/unhandled-length-exception-halfway-through-program

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You might think w is longer than temperature, but it's not. See the comments below.

int [] temperature = new int [depths];
int [] w = new int [depths+5]; // <-- w is depths+5 long

InputValues(temperature);

w = temperature; // <-- w is just another reference to temperature, hence depths long

Array.Copy(temperature, 1, w, 5, 4); <-- This fails.

You cannot allocate a length to an array, then make it point to soemthing else and expect it to keep its length.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...