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
Related
I hava a question about array initialization in java.
The code is as below:
public class Sentence {
int size;
int[] words=new int[size];
public Sentence(int size) {
this.size=size;
}
public static void main(String[] args) {
Sentence falseOne = new Sentence(6);
falseOne.words[0] = new int[6];
}
}
The problem shows: Type provided "int[]", but required "int"
Could anyone tell me where is wrong?
The field words for an object of class Sentence is of type int[] i.e. it is an array whose elements must be of integer type. In the second line within the main function, you are trying to initialize the first element of the words array with an integer array, instead of an integer.
Also, you should also create the array itself within the constructor.
The code should look like this:
public class Sentence {
int size;
int[] words;
public Sentence(int size) {
this.size = size;
this.words = new int[size];
}
public static void main(String[] args) {
Sentence falseOne = new Sentence(6);
falseOne.words[0] = 7; //just picked an arbitrary integer to demonstrate what should be initialized
}
}
This question already has answers here:
Do object arrays in Java have default values?
(6 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I need to assign a value to my 2 Dimension array.
I tried to code as below, but I get NullPointer Exception Error.
MethodClass[][] methodSet = new MethodClass[1][1];
methodSet[0][0].setmethodName(1);
methodSet[0][1].setmethodStatus(1);
The MethodClass file:
public class MethodClass {
private int methodName;
private int methodStatus;
public MethodClass() {
methodName = 0;
methodStatus = 0;
}
public int getmethodName() {
return methodName;
}
public int getmethodStatus() {
return methodStatus;
}
public void setmethodName(int i) {
this.methodName = i;
}
public void setmethodStatus(int status) {
this.methodStatus = status;
}
}
May I know how to initialize the value to 2 dimension array?
The NullPointerException occurs when you try to access a member of a class but the instance of that class is itself containing null value.
To prevent NullPointerException in your case, you must initialize the array values, as for Object type array the default value in positions would be NULL.
Better to do:
MethodClass[][] methodArray = new MethodClass[1][1]; //You can put any dimentions to this array, below for loop will initialize all the positions.
for (int i = 0; i < methodArray.length; i++){
for (int j = 0; j < methodArray[i].length; j++) {
methodArray[i][j] = new MethodClass();
}
}
Then you can access your methods as below:
methodArray[0][0].setmethodName(1);
methodArray[0][0].setmethodStatus(1);
You've initialized the 2D array as 1x1 which means there will only be one element in the array. Trying to use index 1 will throw an error [indexing starts from 0].
If you want to go there, initialize it as a 2x2 array.
You're basically going out of bounds, so if it were an array, you'd get an ArrayIndexOutOfBoundsException.
Initialize it like :
MethodClass[][] methodset = new MethodClass[2][2];
MethodClass[][] methodSet = new MethodClass[1][1] is only initializing your array. To initialize your elements in array you need to do the below code.
MethodClass[][] methodSet = new MethodClass[1][1];
methodSet[0][0]=new MethodClass();
methodSet[0][0].setmethodName(1);
Secondly, for methodset[0][1] you need to change the size or else it will throw ArrayIndexOutOfBoundsException.
MethodClass[][] methodSet = new MethodClass[1][2];
You are declaring a matrix of 1*1, which is of size one. There is only index as (0,0). (0,1) is invalid.
Try using the below code,
MethodClass.java
public class MethodClass {
private int methodName;
private int methodStatus;
public MethodClass() {
methodName = 0;
methodStatus = 0;
}
public MethodClass(int methodName, int methodStatus) {
this.methodName = methodName;
this.methodStatus = methodStatus;
}
public int getMethodName() {
return methodName;
}
public void setMethodName(int methodName) {
this.methodName = methodName;
}
public int getMethodStatus() {
return methodStatus;
}
public void setMethodStatus(int methodStatus) {
this.methodStatus = methodStatus;
}
}
Test.Java
public class Test {
public static void main(String a[]){
MethodClass[][] methodSet = new MethodClass[2][2];
methodSet[0][0] = new MethodClass(0, 0);
methodSet[0][1] = new MethodClass(0, 1);
methodSet[1][0] = new MethodClass(1, 0);
methodSet[1][1] = new MethodClass(1, 1);
System.out.println(methodSet);
}
}
Null pointer exception shows when you try to access a method/function of a class but the instance of that class is itself containing null value.
for example null.setmethodName(1) will throw exception.
The work around in your case is,
First initialize your MethodClass 2D array with the constructor of the class so that, each cell of the 2D array contains MethodClass instance.
MethodClass[][] methodSet = new MethodClass[1][1];
//since the array size of each cell is fixed to 1 in
//your case. So you can loop for row and column from 0 to less then 1 for 0
//indexed cells. otherwise you could also use i<methodSet.length for row and
//j<methodSet[i].length for column
for(int i=0;i<1;i++){
for(int j=0;j<1;j++){
methodSet[i][j]=new MethodClass(); // your MethodClass constructor
}
}
Then you can access your method/function as below:
methodSet[0][0].setmethodName(1); // since the size of each cell of the 2D array is one in your case and index starts from zero so only cell [0][0] is valid in this case
methodSet[0][0].setmethodStatus(1);
Like from {{1.0,2.0},{3.0,4.0}} and {{0.1,0.2},{0.3,0.4}} to {{1.1,2.2},{3.3,4.4}}. If the two input arrays are different sizes, method should return null.
My code below shows [[D#6d06d69c.
What is wrong with my code?
public class Homework13_1 {
public static double[][] sum(double[][]a,double[][]b){
double[][] newA= new double[a.length][a[0].length];
int c=0;
if ((a.length==b.length)){
for (int i=0;i<a.length;i++){
for(int j=0;j<a[0].length;j++){
newA[i][j]=a[i][j]+b[i][j];
}
}
return newA;
}
else{
return null;
}
}
public static void main(String[] args) {
double[][]x={{1,2,3},{2,3,4}};
double[][]y={{2,3,4},{1,1,1}};
double[][] b = {{1,2,-9,7},{3,4,9.9,1.2}};
System.out.println(sum(x, y));
}
}
Java arrays don't override toString. But there is Arrays.deepToString(Object[]) which says (in part) This method is designed for converting multidimensional arrays to strings. You could change
System.out.println(sum(x, y));
to
System.out.println(Arrays.deepToString(sum(x, y)));
I'm trying to create ArrayList with elements from Database. I can get data from DB without any problem, but my problem occurs when I'm trying to create ArrayList of objects. It just doesn't exist. I've declared my list in my main class like this:
private static ArrayList<Vertex> vertexList = new ArrayList<Vertex>();
In my getData() function shown below
private static void getData() throws SQLException{
result = query.executeQuery("SELECT * FROM GTABLE ORDER BY X,Y");
while (result.next()){
int x = result.getInt("X");
int y = result.getInt("Y");
float p = result.getFloat("P");
System.out.printf("X:%d\tY:%d\tP:%.3f\n",x,y,p);
addData(x,y,p);
addData(y,x,p);
}
}
I'm getting all the data from database. Everything works fine until I get to my function addData(x,y,p) shown below:
private static void addData(int x, int y, float p){
Edge edge;
int tmp;
Vertex vertex = new Vertex(x);
edge = new Edge(x,y,p);
//vertex.edgeList.add(edge);
tmp = ifVertexExists(vertex);
if(tmp < 0){
vertexList.add(vertex);
} else {
//vertexList.get(tmp).edgeList.add(edge);
}
}
It just doesn't create anything in my list. I've checked it with isEmpty() and it it was empty.
Any help will be appreciated.
EDIT Here's my Vertex class:
class Vertex{
private final int vertID;
public Vertex(int x){
this.vertID = x;
}
public int getVertID(){
return vertID;
}}
ifVertexExists
private static int ifVertexExists(Vertex vL){
for(Vertex v : vertexList){
if(v.getVertID() == vL.getVertID()){
System.out.printf("Vertex is on the list\n");
return -1;
} else {
System.out.println("No vertex with this ID\n");
}
}
return 0;
}
Right now, I can create the list and it works well, the vertex are shown on screen, tmp is -1. It was bad ifVertexExists, thanks to everyone who helped.
Presumably the response from ifVertexExists is always > 0. Use a debugger or insert print statements to verify this.
I think your vertex class needs to implement equals to be used with ArrayList.contains which you might be using in ifVertexExists.
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.