java - Why last array filled with first array without asking? -
i seek hold value in 1st array, value in 2nd array, seek print 1st array , returns 2nd array values. ideas why ?
this main method phone call program
public class testc { public static void main(string[] args) { groupc trial = new groupc(); trial.setcol(10, 10); } } this class calls class color , fill in array each new coordinate
public class groupc { private color1 col = new color1(); private int[] cola1 = new int[3]; private int[] cola2 = new int[3]; public groupc() { } public void setcol(int xin, int yin) { cola1 = col.getcol(xin, yin); system.out.println(cola1[0] + " " + cola1[1] + " " + cola1[2]); /* seek next color depending on 1st */ cola2 = col.getcol(xin + 100, yin + 100); system.out.println(cola2[0] + " " + cola2[1] + " " + cola2[2]); system.out.println("this 1st color, why same 2nd ?" + cola1[0] + " " + cola1[1] + " " + cola1[2]); } } this class, gets coordinate , homecoming array of color values @ point
import java.awt.color; import java.awt.robot; import java.awt.awtexception; public class color1 { int[] color = new int[3]; public color1() { } public int[] getcol(int xin, int yin) { // accepts position of color, returns size 3 array of reddish greenish bluish // integers seek { robot r = new robot(); color x = r.getpixelcolor(xin, yin); color[0] = x.getred(); color[1] = x.getgreen(); color[2] = x.getblue(); } grab (awtexception e) { e.printstacktrace(); } homecoming color; } }
because color1 reuses single array:
public class color1 { int[] color = new int[3]; // <== creates 1 array color1 instance // ... } all happens in getcol gets filled in (again):
color x = r.getpixelcolor(xin, yin); color[0] = x.getred(); // <==== nil here creating new color array color[1] = x.getgreen(); color[2] = x.getblue(); if want utilize more 1 array, have create more 1 array. mean removing color instance fellow member entirely, , creating array in getcol:
import java.awt.color; import java.awt.robot; import java.awt.awtexception;
public class color1 { public color1() { } public int[] getcol(int xin, int yin) { // accepts position of color, returns size 3 array of reddish greenish bluish // integers int[] color = new int[3]; // has here because of how you're (not) handling exceptions seek { robot r = new robot(); color x = r.getpixelcolor(xin, yin); color[0] = x.getred(); color[1] = x.getgreen(); color[2] = x.getblue(); } grab (awtexception e) { e.printstacktrace(); } homecoming color; } } java arrays
No comments:
Post a Comment