public class warm4{
public static void main(String[] args){
double scale = 3;
double[] array = {1,2,3};
}
public static double[] scalarMultiply(double[] array, double scale){
for( int i=0; i>array.length; i++){
array[i] = (array[i])*scale;
}
return array[]; **//error here!**
}
}
I dont know how to correct that error!
thank you in advance!
return array; **// Now no error here!**
No need for that extra [].
[] is a syntax part of array declaration time to specify length of array.
So need to add that while returning that array. Just use variable name.
array[] doesn't make sense.
If you want to return the value of a variable, just return that variable.
Related
I want to remove elements from an ArrayList in Java and append it to another ArrayList. I am getting boolean cannot be converted to int, x=arr.remove(arr.get(i)); error. Can someone help how to do it?
public class Solution
{
public static void func(ArrayList<Integer> arr, int m)
{
ArrayList<Integer> temp= new ArrayList();
int x;
for(int i=arr.size();i<m;i--){
x=arr.remove(arr.get(i));
temp.add(x);
}
for(int i=0;i<temp.size();i++){
arr.add(temp.get(i));
}
}
}
I am getting error at line 8.
I'm new to this site so let me know if this sort of question is welcome here.
I'm currently coding a class in java to store a set of integers in an array stored in an object and i'm having trouble reassigning the array variable store in the objects created. its not compiling as is(I'm a new programmer so i'm pretty sure i'm missing something simple here).
public class Set
{
// constructor
Set()
{
int array[] = {};
}
//adds a value to the set
public static void addValue(int [] array, int element)
{
int i;
int n = array.length;
int newArray[] = new int[n + 1];
//copy original array into new array
for (i = 0; i < n; i++)
newArray[i] = array[i];
//add element to the new array
newArray[n] = element;
//the issue is this line here
this.array = newArray;
}
//print the set
public static void printSet(int [] array)
{
int i;
int n = array.length;
System.out.print("{");
for (i = 0; i < n; i++)
{
System.out.print(array[i]);
}
System.out.println("}");
}
}
edit - error message returned is:
Set.java:23: error: non-static variable this cannot be referenced from a static context
this.array = newArray;
^
Set.java:23: error: cannot find symbol
this.array = newArray;
^
symbol: variable array
2 errors
First of all, you forgot to put the array inside the class, you can handle it privately like this before the constructor in this way:
private int [] array;
Constructor is used to initialize objects. If you create an empty constructor, you won't be able to pass it any parameters to initialize the array. You can create the constructor this way:
Set (int [] array){
this.array = array;
}
At line you indicated the compiler tells you that you cannot handle the method statically because you are working on an instance method. The "this" keyword is used as a reference to an instance. Since the static methods doesn't have (belong to) any instance you cannot use the "this" reference within a static method. So, you have to remove the static handling from the method. Also, since you want to return the array with the entered value, your method cannot be of type void. So, your method will be:
//adds a value to the set
public int [] addValue(int [] array, int element){
int newArray[] = new int[array.length + 1];
for (int i = 0; i < array.length; i++)
newArray[i] = array[i];
newArray[array.length] = element;
return newArray;
}
Since the length of the array was already available (array.length), it was not necessary to create the variable n, so I took the liberty of making some improvements to your code, to make it less redundant. Similarly, the method you created to print the array would be:
//print the set
public void printSet(int [] array){
System.out.print("{ ");
for (int i = 0; i < array.length; i++){
System.out.print(array[i] + " ");
}
System.out.println("} ");
}
However, once you've made these small changes your code should work fine. You can try to create a testing class like this, to check that everything works right:
public class TestingSet {
public static void main (String [] args){
//creating array
int [] array = {1, 2, 3};
//creating an instance of Set class
Set s = new Set(array);
//printing array
s.printSet(array);
//printing array with new value
s.printSet(s.addValue(array,4));
}
}
It looks like you are trying to access array however it cannot be seen by the addValue method.
You need to declare the array outside of the Set constructor:
public class Set
{
//New location to declear the array
int array[];
// constructor
Set()
{
//We can still initialize the array here
array[] = {};
}
Secondly, the reason for the error is that you cannot use this inside a static method. Static methods are not tied to an object (The Set class) so you need removed static from the method:
//static removed from this line
public void addValue(int [] array, int element)
{
Then to use the method you would create the Set object and use addValue something like this:
Set exampleSet = new Set();
exampleSet.addValue(yourArray, index);
The other option is to make the array a static value (it will no longer be specific to the object but shared with everything), but this is proberbly not the behaviour you want:
public class Set
{
//New location to declear the array
static int array[];
//And to access the object you could use
Set.array = newArray;
I dont understand why this is happening. without the print statement the code works fine but when i try to print the elements i get ArrayIndexoutOfBounds. For example if i try to feed 3 elements i get exception thrown. can anyone please explain
class MyClass
{
int search(OtherClass obs,target) {
double a[]=new double[obs.length];
for(int i=0;i<obs.length;i++)
{
a=obs[i].getTarget();
System.out.println(a[i]);//without this it does not throw
}
}
}
class OtherClass
{
String Target;
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
}
System.out.println(a)
but not
System.out.println(a[i])
In your code obs is an array: obs[i]; each position of that array is, itself, other array: obs[i].getTarget() #=> double[] I guess if, really obshas a method named getTarget() which returns an array.... probably this?
double a[] = new double[obs.length];
double obsArray[] = obs.getTarget();
for(int i=0; i<obs.length; i++){
a[i] = obs[i];
System.out.println(a[i]);
}
You should use this, this statement will set value from obs[i].getTarget() to a[i]
a[i]=obs[i].getTarget();
Edited:
If getTarget() method returns an array then you can this way,
for(int i=0;i<obs.length;i++)
{
double a[] =obs[i].getTarget(); // putting the array from getTarget() to a[]
for(int j=0;j<a.length;j++)
System.out.println(a[j]);//printing all the values of a[]
}
Create a class WrapperDemo that includes a function convert2ObjAndStore that convertsan integer into an Integer object and stores the Integer object in a Vector object namedintVector.Store ānā such converted objects in intVector.Iterate the vector and receive theobject as an integer value and display it.Also include a function that swaps one Integerobject with the other and display the swapped value in the calling routine (main function enclosed in a class named WrapperMain)
I tried this but i am getting an error for converting the interger to Integer object
import java.util.*;
class WrapperDemo {
static void Convert(Integer []ob,int n) {
Vector<Integer> store=new Vector<Integer>();
for(int i=0;i<n;i++) {
store.add(ob[i]);
System.out.println(store.get(i));
}
}
}
class WrapperMain {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.nextLine();
int[] x=new int [20];
for(int i=0;i<n;i++) {
x[i]=sc.nextInt();
WrapperDemo.Convert(x,n);
}
}
}
As far as I can see your error is that you declare x as int[] but pass it to a method that is expecting Integer[]. This will not be autoboxed!
EDIT : To make it clearer ...
class WrapperDemo {
static void Convert(Integer []ob,int n) { // <-- type Integer[]
versus
int[] x=new int [20]; // <-- type int[] which is NOT equal to Integer[]
// and will NOT be autoboxed to Integer[]
// ...
WrapperDemo.Convert(x,n); // You give type int[] to a method that is expecting Integer[]
And as a side-note:
for(int i=0;i<n;i++) {
x[i]=sc.nextInt();
WrapperDemo.Convert(x,n); // <- You call the convertion in each iteration.
}
You call the convertion in each iteration of the filling loop. So most of the entries will be null. You might want to change that to:
for(int i=0;i<n;i++) {
x[i]=sc.nextInt();
}
WrapperDemo.Convert(x,n);
I'm trying to create a 2D array of an image in Java.
Here's what I have so far:
public int[][] GetArray() { //NetBeans is saying 'Illegal Start of Expression' on this line
getimage data;
getwidth;
getheight;
int[][] array = new int[width][height];
for (loop through width) {
for (loop through height) {
array[q][p] = raster.getSample(p, q, 0);
}
}
return array;
I tried setting the return part to:-
return array[][];
but that produced an error saying cannot find symbol.
I'm relatively new to Java and I really want to get better quickly, if you could help me out, I'd really appreciate it.
If you'd like to return an array, do it like this
return array; // CORRECT
What you are doing is incorrect.
return array[][]; // INCORRECT
Your function should look like this
public class MyClass
{
// main is a method just like GetArray, defined inside class
public static void main(String[] args)
{
// do something
}
// other methods are defined outside main but inside the class.
public int[][] GetArray() {
int width = 5; // change these to your dimensions
int height = 5;
int[][] array = new int[width][height];
int q,p;
for(q=0;q<width;q++)
{
for(p=0;p<height;p++)
{
array[q][p] = raster.getSample(p, q, 0);
}
}
return array;
}
}
when you return an array you should not use
return array[][];
you should not use the square brackets [][] . In the return statement we should not mention the dimensions of the array
instead use
return array; this is the correct way