khris
khris

Reputation: 4999

How set values of array as parameters?

I want call method path from class Path. I need set some number - array length and appropriate values of array on entrance. This is my class:

class Path {
 int number;
 String[] path_name = new String [number];
 Path (int n, String [] p){
     number=n; 
     path_name=p; 
 }
public void path(){
 for (int i=1; i<number; i++){
     driver.findElement(By.linkText(path_name[i])).click();  
 }
}

}

This is how I try call method:

Path pa = new Path (6,'array value');
  pa.path();

But I don't know how properly initiate 'array value' - I need set "one", "two","three" for example. Could someone help?

I do not know length of array on the beginning, so I can't define several Strings in the constructor

Upvotes: 0

Views: 141

Answers (5)

corsiKa
corsiKa

Reputation: 82579

The n number you refer to can be eliminated if you accept the invariant that all elements of the array are valid elements to path for.

With this in mind, you can simply have

class Path {
    final String[] pathNames;
    // uses var args
    Path(String... paths) {
         pathNames = paths;
    }
    void path() {
        // uses enhanced for loop
        for(String path : pathNames) {
            driver.findElement(By.linkText(path)).click();
        }
    }
}

Some critiques:

  • You currently are creating an array of size number when you create the object. But number is 0 at that point. So you're creating an empty array.
  • But the above point is irrelevant because you're immediately reassigning the reference...
  • Because you're not defensively copying the array, some external code could modify the elements of the array. This is probably not a good thing. (This is addressed by the var args, where no outside code has access to that array. You can't stop someone from passing an array in instead of var args, but they will at least be aware of the fact that you expect var args, so they still have the ability to alter internally if they want to, but are unlikely to accidentally introduce a bug in such a manner.)

Upvotes: 0

Sterling
Sterling

Reputation: 4213

If I understand what you're asking right, I think this is what you want -

class Path { 
 int number; 
 String[] path_name; 
 Path (int length){ 
     path_name = new String[length];
     for(int i=1;i<=length;i++)
         path_name[i-1] = Integer.ToString(i); 
 } 
} 

This assumes you want the array to be 1...n. If you want to have any string elements you want, and I think this is probably a better solution, I would do

 class Path { 
 int number; 
 String[] path_name; 
 Path (String p){ 
     path_name = p;
 }


 public void some_method(){ 
    String[] a = {"one", "two", "three"};
    Path p = new Path(a);
 }
 } 

Upvotes: 0

Nitram
Nitram

Reputation: 6716

There are multiple problems in your class.

First of the initialization of variables that is written directly at the class variables is done before the constructor body is executed. So your initialization of the path_name array will always by done with the default value of number and that is 0. This works but your array won't be able to store any values this way.

Now in your body, I guess you try to fill your path_name array with the content of the p array. But instead you replace the instance. This works just fine but in case anyone changes the array outside the class it will change in this class as well. I guess you want to transfer the values using System.arraycopy or Arrays.copyOf.

For the call of your constructor you have to call something like this:

new Path(6, new String[] {"one", "two", ...});

As improvement I suggest you drop the number variable entirely how ever because you can at all time refer to the size of the array itself using path_name.length.

Another thing you might want to look into is variable length arguments. If you change your constructor like this:

Path(int n, String p...){
     number=n; 
     path_name = p
}

You set that there is a undefined amount of strings to be added after the number in the call of the constructor. All this strings are automatically packed in a String array called p. As this array only exists inside this constructor you can safely copy the instance instead of the variables. The call of the constructor then looks like this:

new Path(6, "one", "two", ...);

Upvotes: 2

jrad
jrad

Reputation: 3190

You could initialize a String array prior to the Path instantiation like this:

String[] p = {"one", "two", "three"};
Path pa = new Path(6, p);

Upvotes: 0

Robert
Robert

Reputation: 2471

Try something like:

Path pa = new Path(6, String[] {"one", "two", "three"} );

If you can change the code you can probably use varargs to make it easier:

Path (int n, String p...) 

Then you could just to:

Path pa = new Path(6, "one", "two", "three");

Upvotes: 1

Related Questions