数组不能加整数

时间:2019-01-15 14:35:26

标签: c# arrays sorting

我尝试将元素添加到数组,但是当我尝试添加元素时,它显示array为null。我在第一个元素的数组中添加了一个整数,但是仍然遇到问题。有人可以建议吗?

class unorderedArray
{
    public int[] itemArray=null ;
    private int numElements=0;
    private int maxElements=0;
    public unorderedArray(int max)
    {
        maxElements = max;
       int[] itemArray=new int[maxElements]  ;
    }
    public int[] getItemArray()
    {
        return itemArray;
    }
    public bool addLast(int item)
    {
        if (numElements < maxElements)
        {
            itemArray[numElements] = item;
            numElements++;

        }

        return true;
    }
}


    class Program
{
    static void Main(string[] args)
    {
        unorderedArray aa = new unorderedArray(60);
        aa.addLast (4);
        aa.addLast(5);
        aa.addLast(6);
        aa.addLast(8);
        aa.addLast(90);
        aa.addLast(12);
        aa.addLast(77);
        aa.printList();
        Console.ReadKey();

    }
}

enter image description here

2 个答案:

答案 0 :(得分:0)

function openTask(taskId) {
    $.post(http://localhost:1000/endpoint/, function (data) {

        if (data !== "Failed") {
            if (data.externalProvider === 1) {
                //Change button design here
                $('div.modal-footer button', '#EditTask').text([...your desired text...]);
            } else {
                //Keep button design the same
            }

        } else {
            alert("An error occured");
        }
    });

    $('#EditTask').modal('toggle');
}

... MDLAsset *asset = [[MDLAsset alloc] initWithURL:[[NSBundle mainBundle] URLForResource:@"template" withExtension:@"obj"]]; MDLTextureSampler *sampler = [[MDLTextureSampler alloc] init]; sampler.texture = [MDLTexture textureNamed:@"texture.png"]; MDLMaterialProperty *property = [[MDLMaterialProperty alloc] initWithName:@"baseColor" semantic:MDLMaterialSemanticBaseColor textureSampler:sampler]; [[[[(MDLMesh*)[asset objectAtIndex:0] submeshes] objectAtIndex:0] material] setProperty:property]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString* usdcPath = [documentsDirectory stringByAppendingPathComponent:@"model.usdc"]; NSURL *usdcUrl = [NSURL fileURLWithPath: usdcPath]; if ([MDLAsset canExportFileExtension:@"usdc"]) { [asset exportAssetToURL:usdcUrl]; } 不同。

如果您写public int[] itemArray=null ; // <============== THIS public unorderedArray(int max) { maxElements = max; int[] itemArray=new int[maxElements] ; // <=== AND THIS ... } ,则是在声明一个隐藏类字段的局部变量。

只需从构造函数内的“ int [] itemArray ...”中删除“ int []”。

不相关:请遵循约定在CamalCase中命名类。 =>“ UnorderedArray”

答案 1 :(得分:0)

您要重新声明数组,从而使其成为局部数组,但不应如此 这个

public unorderedArray(int max) {
     maxElements = max; 
     int[] itemArray=new int[maxElements] ;
} 

应该是

 public unorderedArray(int max) {
     maxElements = max; 
     itemArray=new int[maxElements] ;
} 
相关问题