O
O
onex_dev2019-05-25 21:48:23
Java
onex_dev, 2019-05-25 21:48:23

Unable to convert double [] to class Main how to solve?

How to find out what type Main has and why it cannot cast it?
Throws an error:
Main.java:26: error: incompatible types: double[] cannot be converted to Main

public class Main
{
   private double [] vektor; 
   public Main() {
       double [] vektor = {2, 3.5, -5}; 
    }
   public  Main plus (Main vektor){
        double [] a = {2, 3.5, -5};
        double [] b = {2, 3.5, -5};
        double [] r = new double [a.length+b.length];
        System.arraycopy(a, 0, r, 0, a.length);
  System.arraycopy(b, 0, r, a.length, b.length);
  return r;
    }
 }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2019-05-25
@GavriKos

Main has type Main.
It cannot result because there are no rules for transferring an array of doubles to main.
Question - what do you want from the Plus method? It does not use any input parameter, and for some reason it returns Main. The code is weird to say the least.

M
Marat Khayrutdinov, 2019-05-27
@distrik

First part of the code

private double [] vector;
public Main() {
double [] vector = {2, 3.5, -5};
}

has nothing to do with the second part
public Main plus (Main vector){
double [] a = {2, 3.5, -5};
double[] b = {2, 3.5, -5};
double [] r = new double [a.length+b.length];
System.arraycopy(a, 0, r, 0, a.length);
System.arraycopy(b, 0, r, a.length, b.length);
return r;
}

Moreover, when declaring a method:
you said that you would need a parameter of type Main to work, but you didn't use it anywhere in the body.
Also, you asked to return an object of type Main, but you return an array.
Either promise to honestly return an array
Or put an array inside Main via the constructor
public Main(double [] vector) {
this. vector = vector;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question