How many dimensions in an array with no value

I'm a little lost (still working with Ron Jeffries's book). Here's a simple class:

public class Model{
    private String[] lines;

    public void myMethod(){
        String[] newLines = new String[lines.length + 2];
        for (i = 0, i <= lines.length, i++) {
            newLines[i] = lines[i];
        }
    }
}

I have another class that initializes Model, and an empty array, by setting myModel = new String[0]. When I invoke myModel.myMethod(), I get a subscript out of range error. Looking at the debugger, what I see is that myModel.lines has zero dimensions and zero length. Shouldn't it have a dimension and length of 1? Granted the value of lines[0] is null, but the array itself shouldn't be, should it?

Any thoughts truly appreciated.

Randy

Answers


I think your example is probably not the same as your actual code based on your description. I think the problem is that arrays are zero-based and thus an array initialized as:

string[] lines = new string[0];

has no elements.

You need to change your loop so that you check that the index is strictly less than the length of the array. As others have indicated you also need to make sure that the array itself is not null before trying to reference it.

My take on your code:

public class Model{
    private String[] lines = new string[0];

    public Model( string[] lines ) {
        this.lines = lines;
    }

    public void myMethod(){
        int len = 2;
        if (lines != null) {
            len = len + lines.length;
        }
        String[] newLines = new String[len];
        for (i = 0, i < lines.length, i++) {
            newLines[i] = lines[i];
        }
    }
}

lines will be null, so lines.length will throw an exception.

I believe your other class initializing "Model" won't help since Lines itself is private. In fact, whatever you are doing to Model is probably illegal in at least 30 states.


lines is initalized to null, check for null or initialize it in this way :

private String[] lines = new String[0];

You cannot initialize an instance of Model by setting it equal to a String array. I'm actually surprised that the compiler will let you even try.

If you really want Model to be initializable with an external array, you should make a Constructor for the Model class that will take as an argument the array. Then in the body of your constructor, set the value of lines to that value.

Example:

public class Model {
    private String []lines;

    public Model(String [] inLines)
       {
       lines = inLines;
       }
}

Usage:

myStringArray = new String[0];
myModel = new Model(myStringArray);

Need Your Help

How to check if some words are in a string or not?

java regex string

What I want to do is checking if some words are in a string or not.

Have problems installing mod_python

django apache deployment apache2 mod-python

I seem to have some problems installing mod_python with apache.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.