Hi all, I'm actually equating a 2D array from a class to another class. I call it using method call. However, it seems like the 2D array is empty after I equate it to another 2D array in another class. May I know where is the problem?

import java.io.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
class MatchApplet extends JApplet {
	TextArea outputArea;
	JButton button;
	JButton reset;
	JTextField tF1;
	JTextField tF2;
	JLabel l1;
	JLabel l2;
	String s1;
	String s2;
	static String display1 = "";
  	static String display2 = "";
static double[][] myDouble = new double[1000][1000];
	static int x=0, y=0;
	public static void main(String[] args)
            //Application for program
            Frame f = new Frame();
            f.addWindowListener(new java.awt.event.WindowAdapter()
                public void windowClosing(java.awt.event.WindowEvent e)
  		MatchApplet ut = new MatchApplet();
  		ut.setSize(900,900); // same size as defined in the HTML APPLET
  		f.setSize(900,900 + 100); // add 20, seems enough for the Frame title,
		//end of application for program
	}//end of public static void main(String[] args)
  	public void init()
		BufferedReader in1 = new BufferedReader(new FileReader("C:\\Users\\Serene\\Documents\\Major Project\\Alignment Algorithms\\1a00(50).txt"));	//reading files in specified directory
		BufferedReader in2 = new BufferedReader(new FileReader("C:\\Users\\Serene\\Documents\\Major Project\\Alignment Algorithms\\1a0a(50).txt"));	//reading files in specified directory
		BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\Serene\\Documents\\Major Project\\Alignment Algorithms\\Testing2.txt"));	//reading files in specified directory
  			String str1;
			while ((str1 = in1.readLine()) != null)	//file reading
				display1 = str1;
			String str2;
			while ((str2 = in2.readLine()) != null)	//file reading
				display2 = str2;
			String line;
			while ((line = in.readLine()) != null)
				String[] values = line.split(",");
	        	for (String str : values)
	        		double str_double = Double.parseDouble(str);
	        		myDouble[x][y] = str_double;
		Container c = getContentPane();
		c.setLayout(new FlowLayout());
		outputArea = new TextArea(40,110);
		Font font = new Font("Courier", Font.PLAIN, 12);
		tF1 = new JTextField(display1);
		tF2 = new JTextField(display2);
		l1 = new JLabel("Sequence 1:");
		l2 = new JLabel("Sequence 2:");
	final Substitution sub = new Blosum50();
		s1 += tF1.getText();
		s2 += tF2.getText();
		Output out = new Output ()
	  		public void print(String s)
	  		{ outputArea.append(s); }
	  		public void println(String s)
	  		{ outputArea.append(s); outputArea.append("\n"); }
	  		public void println()
	  		{ outputArea.append("\n"); }
      (new NW      (sub, 8,     s1, s2)).domatch(out, "GLOBAL ALIGNMENT");
      (new SW      (sub, 8,     s1, s2)).domatch(out, "LOCAL ALIGNMENT");
      }catch( IOException ioException ) {}
  }//end of init()
      static double[][] getMyDouble()
      { return myDouble; }
}//end of class
// The class of substitution (scoring) matrices
abstract class Substitution {
	public double[][] score;
	void buildscore(String residues, String residues2, double[][] residuescores)
	// Allow lowercase and uppercase residues (ASCII code <= 127):
	score = new double[127][127];
	for (int i=0; i<residues.length(); i++)
		char res1 = residues.charAt(i);
		for (int j=0; j<=i; j++)
        char res2 = residues2.charAt(j);
        score[res1][res2] = score[res2][res1]
	  = score[res1][res2+32] = score[res2+32][res1]
	  = score[res1+32][res2] = score[res2][res1+32]
	  = score[res1+32][res2+32] = score[res2+32][res1+32]
	  = residuescores[i][j];
	abstract public String getResidues();
	abstract public String getResidues2();
// The BLOSUM50 substitution matrix for amino acids (Durbin et al, p 16)
class Blosum50 extends Substitution
	private String residues = MatchApplet.display1;
	private String residues2 = MatchApplet.display2;
	public String getResidues()
	{ return residues; }
	public String getResidues2()
	{ return residues2; }
	private double[][] residuescores = MatchApplet.getMyDouble();
	public Blosum50()
	{ buildscore(residues, residues2, residuescores); }

I actually want the stored values in the 2D array in line 96, to be brought over to the 2D array in line 203. I used method call in line 152 and 153 to return the 2D array myDouble, and equate the method call to the 2D array in line 203. However, I realised my 2D array in line 96 is empty after it is out of the for loop. May I know how can I still store the values in it and call it elsewhere even when it is out of the for loop?

Because you are using iterator but you never increase value of x and y you are using in the loop.

Insert y=0; before line 92
Insert y++; after line 96
Insert x++; before line 98

Ya okay that is done. But when I access my 2D array out of that loop it doesn't work anymore.

So did you print it out after the loop to see that the value exists? If yes, but when you call it from other class, what error you got?

If there is still a problem, you could try to create a copy of the array and return it.

Edited 6 Years Ago by Taywin: n/a

I tried making a copy of the array, but isn't it the same? Hmmm, it just can't seem to work.

This question has already been answered. Start a new discussion instead.