不工作的权利整数ArrayList的Java的ArrayList的 [英] Java ArrayList of ArrayList of integers not working right

查看:91
本文介绍了不工作的权利整数ArrayList的Java的ArrayList的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作是有大量的整数工程项目,我决定使用ArrayList< ArrayList的<整数>>所谓ALL_PIXELS。我无法让我在我的ArrayList或值的ArrayList是唯一的。当我检查code一些基本的测试在for循环一切正常,一旦出了子数组的for循环的所有值都是一样的,他们不应该。这里是一些code我有,也许有人可以帮助我。
我已经做了很多的System.out语句测试,并让他们注释掉。我发现这个问题是我的ArrayList ALL_PIXELS似乎里面确定为循环,但之后的for循环中的所有子阵都一样这是一个问题。

I am working on a project that works with a large number of integers, i decided to use an ArrayList< ArrayList< Integers>> called ALL_PIXELS. i am having trouble getting my values in my arrayList or Arraylists to be unique. When i check the code with some basic test in the for loop everything is ok, once out of the for loop all the values of the sub array are the same, which they should not be. here is some code i have, maybe someone can help me. I have done a lot of testing with system.Out statements and have commented them out. i have found the problem to be that my ArrayList ALL_PIXELS seems to ok inside the for loop but after the for loop all subarrays in it are the same which is a problem.

package rmi.client;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageProducer;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import java.util.HashMap;

import rmi.Compute;
import rmi.RmiStarter;

public class StartComputeTaskMan extends RmiStarter {

// things for the Panel that i can't extend

private static JPanel mainPanel = new JPanel(); // this is what I'll add tocontentPane                                               
private static JPanel panel = new JPanel();
private JComponent[] allComponents = { panel };

final static int FRAME_WIDTH = 2000;
final static int FRAME_HEIGHT = 1500;
final static int MAX_ITERATION = 1000;
final static double ZOOM_FACTOR = 5;

final static double X_LEFT_BOUND = -2.5;
final static double X_RIGHT_BOUND = 1.5;
final static double Y_TOP_BOUND = 1.5;
final static double Y_BOTTOM_BOUND = -1.5;

final static BufferedImage IMAGE = new BufferedImage(FRAME_WIDTH,FRAME_HEIGHT, BufferedImage.TYPE_INT_RGB);
final static int[] COLORS = new int[MAX_ITERATION];
final static ArrayList<ArrayList<Integer>> ALL_PIXELS = new ArrayList<ArrayList<Integer>>();
ArrayList<HashMap<String, Double>> boundsHistory = new ArrayList<HashMap<String, Double>>();
int zoomNum = 0;

// end of global variables

// constructor
public StartComputeTaskMan() {
    super(PI.class);


    for (JComponent comp : allComponents) {
        mainPanel.add(comp);
    }// end for loop

    HashMap<String, Double> map = new HashMap<String, Double>();
    map.put("left", X_LEFT_BOUND);
    map.put("right", X_RIGHT_BOUND);
    map.put("top", Y_TOP_BOUND);
    map.put("bottom", Y_BOTTOM_BOUND);
    boundsHistory.add(map);

    for (int i = 0; i < MAX_ITERATION; i++) {
        COLORS[i] = Color.HSBtoRGB(i / 256f, 1, i / (i + 8f));
    }// end for loop
}// end constructor

public static JComponent getMainComponent() {
    return mainPanel;
}// end JComponent

private static void createAndShowGui() {

    // creating my JFrame only when I need it and where I need it
    JFrame frame = new JFrame("Mandelbrotset");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(getMainComponent());
    frame.pack();
    frame.setLocationByPlatform(true);
    frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    frame.setVisible(true);
    panel.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    panel.setVisible(true);
    frame.add(panel);
}

@Override
public void doCustomRmiHandling() {
    try {
        System.setProperty("java.rmi.server.hostname", "172.31.98.63"); 
        System.out.println("Getting Registry");
        Registry registry = LocateRegistry.getRegistry("172.31.98.63"); 
        System.out.println("Getting Compute");
        try {
            Compute<Integer> compute = (Compute<Integer>) registry.lookup(Compute.SERVICE_NAME);            
        } catch (NotBoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}//end doCustomRmiHandling

public void doTask() {
    System.out.println("Getting Task");


    //why is this not making ALL_PIXELS unique for each row .............

    for (int i = 0; i < FRAME_HEIGHT; i++) {

        HashMap<String, Double> t = boundsHistory.get(zoomNum);
        PI task = new PI(i, t);
        //this is the problem. some where 
        ArrayList<Integer> temp = new ArrayList<Integer>();
        temp = task.execute();
        //System.out.println(temp);
        ALL_PIXELS.add(temp);
        //System.out.println(ALL_PIXELS.get(i));     //still unique here....WHYYYYYYY
        //System.out.println(temp);     //this shows that temp is a unique array each row

    }// end for loop    



    //why is all pixels seting the same vales to all of its sub arrays
    //System.out.println(ALL_PIXELS.get(3));
    //System.out.println(ALL_PIXELS.get(300));


    System.out.println("Computing");




}// end doTask



public double getScaledX(int x) {
    double returnValue = ((double) x / (double) FRAME_WIDTH)* (boundsHistory.get(zoomNum).get("right")
            - boundsHistory.get(zoomNum).get("left"))+ boundsHistory.get(zoomNum).get("left");      
    return returnValue;
}// end getscaledX

public double getScaledY(int y) {
    double returnValue = ((double) y / (double) FRAME_HEIGHT)* -(boundsHistory.get(zoomNum).get("top") 
            - boundsHistory.get(zoomNum).get("bottom")) + boundsHistory.get(zoomNum).get("top");    
    return returnValue;
}// end getscaledY

public static void main(String[] args) {
    createAndShowGui();//makes our GUI
    StartComputeTaskMan test = new StartComputeTaskMan();
    test.doTask();//fills in ALLPIXELS 

    //all of sub arrays are the same in all pixels



    //makeImage(test);//makes the buffered image
    //paintComponent(null);

}// end main

private static void makeImage(StartComputeTaskMan test) {
    for(int j=0; j<FRAME_HEIGHT;j++){
        for(int i=0;i<FRAME_WIDTH;i++){
            if(ALL_PIXELS.get(j).get(i)<MAX_ITERATION){
                IMAGE.setRGB(j,i,COLORS[ALL_PIXELS.get(j).get(i)-1]);
            }else{
                IMAGE.setRGB(j,i,0);

            }


        }//end nested for loop
    }//end for loop
}//end makeImage



private static void paintComponent(Graphics g){
    g.drawImage(IMAGE, 0, 0, FRAME_WIDTH, FRAME_HEIGHT,null);
}
}// end class

这是我的主要code。我有第二个方法是这样的。所有这一切都有大约7的其他类,使RMI可能。但这些都是不需要的,我有这个问题。

That is my main code. The second method that I have is this. all of this has about 7 other classes that make RMI possible. but those are not needed for this problem that i am having

package rmi.client;
                                                            //rmi client
import java.awt.image.BufferedImage;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;


import rmi.Task;

public class PI implements Task<BigDecimal>, Serializable {

    private static final long serialVersionUID = 3942967283733335029L;

    final static int FRAME_WIDTH = 2000;
    final static int FRAME_HEIGHT = 1500;
    final static int MAX_ITERATION = 1000;
    final static double ZOOM_FACTOR = 5;

    final static double X_LEFT_BOUND = -2.5;
    final static double X_RIGHT_BOUND = 1.5;
    final static double Y_TOP_BOUND = 1.5;
    final static double Y_BOTTOM_BOUND = -1.5;

    final static BufferedImage IMAGE = new BufferedImage(FRAME_WIDTH, FRAME_HEIGHT, BufferedImage.TYPE_INT_RGB);
    final static int[] COLORS = new int[MAX_ITERATION];
    //final static ArrayList<ArrayList<Integer>> ALL_PIXELS = new ArrayList<ArrayList<Integer>>(FRAME_HEIGHT);
    static ArrayList<HashMap<String, Double>> boundsHistory = new ArrayList<HashMap<String, Double>>();
    int zoomNum = 0;

    static ArrayList<Integer> rowList = new ArrayList<Integer>();

    static double scaledRow;
    static int row;
    static int frame_width = FRAME_HEIGHT;
    static int frame_height;
    static int max_iteration;
    static int[] colors;
    static HashMap<String, Double> bounds;
    static BufferedImage image;




public PI(int row, HashMap<String,Double> bounds){
        rowList.clear();//just to be safe
        this.row = row;
        this.bounds = bounds;
        scaledRow = getScaledY(row);

        frame_width = FRAME_WIDTH;
        frame_height = FRAME_HEIGHT;
        max_iteration = MAX_ITERATION;
        image = IMAGE;
        colors = COLORS;
    }

    /**
     * sending arrays to the rmi base and then to the server
     */
    public ArrayList<Integer> execute() {

        rowList = computePi(row);
        //System.out.println(rowList);
        return rowList;
    }
    //this is what is doing the work
    public static  ArrayList<Integer> computePi(int row) {



        for(int column = 0; column<frame_width; column++){
            double x0 = getScaledX(column);
            double y0 = getScaledY(row);
            double x = 0.0;
            double y = 0.0;
            int iteration = 0;

            while((x*x + y*y < 2*2) && iteration <max_iteration){
                double xtemp = x*x - y*y + x0;
                y = 2*x*y + y0;
                x = xtemp;
                iteration++;
                }
    //System.out.println("row number "+row + ": column number " + column+ ": " + " iterations "+iteration);
                if(iteration < max_iteration){

                    //image.setRGB(column,row,colors[iteration-1]);
                    rowList.add(iteration-1);
                    } else{
                        //image.setRGB(column,row,0);
                    rowList.add(0);
                    }
            }
        return rowList;

    }//end compute pi

        //MandelbrotSet2.ALL_PIXELS.add(row, rowList);

        public static double getScaledX(int x){
            double returnValue = ((double)x/(double)frame_width)*(bounds.get("right") - bounds.get("left")) + bounds.get("left");
            return returnValue;
        }

        public static double getScaledY(int y){
            double returnValue = ((double)y/(double)frame_height)*-(bounds.get("top") - bounds.get("bottom")) + bounds.get("top");
            return returnValue;
        }

        public void run() {
            computePi(row);
        }



    }

再论为什么ALL_PIXELS不是for循环将是不错独特之外的任何建议。我试图调试它,我找不到为什么我有一个问题。

Again any advice on why ALL_PIXELS is not unique outside of the for loop would be nice. I have tried to debug it and i could not find why i am having a problem.

推荐答案

试着改变如下行:

    ArrayList<Integer> temp = new ArrayList<Integer>();
    temp = task.execute();
    //System.out.println(temp);
    ALL_PIXELS.add(temp);

    ArrayList<Integer> temp = null;
    temp = task.execute();
    //System.out.println(temp);
    ALL_PIXELS.add(new ArrayList<Integer>(temp));

作为PI类的execute方法正在为computePi调用它正在修改其哟添加到列表ALL_PIXELS ArrayList中引用()方法。所以相同的附图是越来越改性每一次。
因此,在这时候你把它添加到您的ALL_PIXELS列表,请确保您有一个新的列表引用。

As the execute method of PI class is making a call to computePi() method which is modifying the arraylist reference which yo added to the ALL_PIXELS list. So the same reference is getting modified every time. So at the time you add it to your ALL_PIXELS list, make sure you have a new list reference.

这篇关于不工作的权利整数ArrayList的Java的ArrayList的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆