Calling Arrays

Calling Arrays from other files.

Page 1 of 1

6 Replies - 921 Views - Last Post: 24 July 2008 - 04:52 AM Rate Topic: -----

#1 j0e  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 46
  • Joined: 01-June 08

Calling Arrays

Posted 22 July 2008 - 10:10 AM

Hi is there a way to call an array from a class file to another file just like we can do with methods by creating new objects?Pls provide me a simple example or pseudocode if it is possible.
Is This A Good Question/Topic? 0
  • +

Replies To: Calling Arrays

#2 JeroenFM  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 18
  • View blog
  • Posts: 195
  • Joined: 30-June 08

Re: Calling Arrays

Posted 22 July 2008 - 11:46 AM

View Postj0e, on 22 Jul, 2008 - 10:10 AM, said:

Hi is there a way to call an array from a class file to another file just like we can do with methods by creating new objects?Pls provide me a simple example or pseudocode if it is possible.


What exactly do you mean by "call an array"?
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Calling Arrays

Posted 22 July 2008 - 06:20 PM

View Postj0e, on 22 Jul, 2008 - 10:10 AM, said:

Hi is there a way to call an array from a class file to another file just like we can do with methods by creating new objects?Pls provide me a simple example or pseudocode if it is possible.


class MyClass {
	private int[] myArray;
	....
	....
	.....
	public int[] getArray() {
	   return myArray;
   }
}


Was This Post Helpful? 0
  • +
  • -

#4 j0e  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 46
  • Joined: 01-June 08

Re: Calling Arrays

Posted 23 July 2008 - 09:58 AM

Thanks for the answers, i am new to Object Oriented Programming so i will try to be more clear.I want to create a User Interface for an ATM machine using NetBeans. I have two classes .

The one is Bank.

import java.util.ArrayList;

/**
 *
 * @author j0e
 */
public class Bank {
	
	public Bank()
	{
		accounts = new ArrayList<BankAccount>();
	}
	
	public void addAccount(BankAccount a)
	{
		accounts.add(a);
	}
	
	public BankAccount find(int accountNumber)
	{
		for (BankAccount a : accounts)
		{
			if (a.getAccountNumber() == accountNumber)
				
				return a;
		}
		return null;
		}
	
	private ArrayList<BankAccount> accounts;
	}




and the other is BankAccount.

public class BankAccount
{  
   /**
	  Constructs a bank account with a zero balance.
   */
   public BankAccount(int anAccountNumber)
   {   
	  accountNumber = anAccountNumber;
	  balance = 0;
   }

   /**
	  Constructs a bank account with a given balance.
	  @param initialBalance the initial balance
   */
   public BankAccount(int anAccountNumber, double initialBalance, String bank)
   {  
	  accountNumber = anAccountNumber; 
	  balance = initialBalance;
	  accountBank = bank;
   }
   
   public int getAccountNumber()
   {
	   return accountNumber;
   }

   public String getBank()
   {
	   return accountBank;
   }
   /**
	  Deposits money into the bank account.
	  @param amount the amount to deposit
   */
   public void deposit(double amount)
   {  
	  double newBalance = balance + amount;
	  balance = newBalance;
   }
   
   /**
	  Withdraws money from the bank account.
	  @param amount the amount to withdraw
   */
   public void withdraw(double amount)
   {   
	  double newBalance = balance - amount;
	  balance = newBalance;
   }

   /**
	  Gets the current balance of the bank account.
	  @return the current balance
   */
   public double getBalance()
   {   
	  return balance;
   }

   private String accountBank;
   private int accountNumber;
   private double balance;
}



The implementation class is Atm.

import javax.swing.JOptionPane;
import mypackage.BankAccount;

/**
 *
 * @author  j0e
 */
public class Atm extends javax.swing.JDialog {
	

 
	/** Creates new form Atm */
	public Atm(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();		
	}
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">						  
	private void initComponents() {

		mainPanel = new javax.swing.JPanel();
		Panel1 = new javax.swing.JPanel();
		CodeField = new javax.swing.JTextField();
		jLabel1 = new javax.swing.JLabel();
		button = new javax.swing.JButton();
		Panel2 = new javax.swing.JPanel();
		backButton = new javax.swing.JButton();
		jButton1 = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		mainPanel.setLayout(new java.awt.CardLayout());

		CodeField.setFont(new java.awt.Font("Tahoma", 0, 48));

		jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
		jLabel1.setText("Enter your Six Digit Code");

		button.setFont(new java.awt.Font("Tahoma", 0, 18));
		button.setText("Enter");
		button.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				buttonActionPerformed(evt);
			}
		});

		org.jdesktop.layout.GroupLayout Panel1Layout = new org.jdesktop.layout.GroupLayout(Panel1);
		Panel1.setLayout(Panel1Layout);
		Panel1Layout.setHorizontalGroup(
			Panel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
			.add(org.jdesktop.layout.GroupLayout.TRAILING, Panel1Layout.createSequentialGroup()
				.add(160, 160, 160)
				.add(Panel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
					.add(Panel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
						.add(CodeField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 198, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE))
					.add(Panel1Layout.createSequentialGroup()
						.add(32, 32, 32)
						.add(button, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 129, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
				.add(107, 107, 107))
		);
		Panel1Layout.setVerticalGroup(
			Panel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
			.add(org.jdesktop.layout.GroupLayout.TRAILING, Panel1Layout.createSequentialGroup()
				.addContainerGap(116, Short.MAX_VALUE)
				.add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 46, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
				.add(CodeField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 48, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
				.add(26, 26, 26)
				.add(button, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 43, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
				.add(105, 105, 105))
		);

		mainPanel.add(Panel1, "card2");

		backButton.setFont(new java.awt.Font("Tahoma", 0, 18));
		backButton.setText("Go Back");
		backButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				backButtonActionPerformed(evt);
			}
		});

		jButton1.setText("100");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		org.jdesktop.layout.GroupLayout Panel2Layout = new org.jdesktop.layout.GroupLayout(Panel2);
		Panel2.setLayout(Panel2Layout);
		Panel2Layout.setHorizontalGroup(
			Panel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
			.add(org.jdesktop.layout.GroupLayout.TRAILING, Panel2Layout.createSequentialGroup()
				.addContainerGap(406, Short.MAX_VALUE)
				.add(backButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 135, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
				.addContainerGap())
			.add(Panel2Layout.createSequentialGroup()
				.add(124, 124, 124)
				.add(jButton1)
				.addContainerGap(376, Short.MAX_VALUE))
		);
		Panel2Layout.setVerticalGroup(
			Panel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
			.add(org.jdesktop.layout.GroupLayout.TRAILING, Panel2Layout.createSequentialGroup()
				.add(106, 106, 106)
				.add(jButton1)
				.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 205, Short.MAX_VALUE)
				.add(backButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 42, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
				.addContainerGap())
		);

		mainPanel.add(Panel2, "card3");

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
			layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
			.add(mainPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
		);
		layout.setVerticalGroup(
			layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
			.add(mainPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
		);

		pack();
	}// </editor-fold>						

private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {										   
Panel2.setVisible(false);
Panel1.setVisible(true);
}										  

private void buttonActionPerformed(java.awt.event.ActionEvent evt) {									   

  try{
   String accountNumber = CodeField.getText();
   int value = Integer.parseInt(accountNumber);
   
	 Bank  Bank = new  Bank();
		Bank.addAccount(new BankAccount(100, 1000, "Pireos"));
		Bank.addAccount(new BankAccount(200, 2000, "City"));
		Bank.addAccount(new BankAccount(300, 3000, "Pireos")); 
	   
   
 BankAccount a = Bank.find(value);			
 if (a == null){
   System.out.println("No Account with that number");
 }
 else{
	Panel1.setVisible(false);
	Panel2.setVisible(true);
	System.out.println("Account with number " + value + "  has balance " +
			 a.getBalance() + " The bank of this Account is " + a.getBank());
 }
 }catch(Exception ex){
	  JOptionPane.showMessageDialog(this,"Please type a number in the box");
  } 
	
}									  

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {										 
 

			   
}										

	/**
	* @param args the command line arguments
	*/
	public static void main(String args[]) {
	   
	   
	   
		
		java.awt.EventQueue.invokeLater(new Runnable()   {
			public void run() {
				Atm dialog = new Atm(new javax.swing.JFrame(), true);
				dialog.addWindowListener(new java.awt.event.WindowAdapter() {
					@Override
					public void windowClosing(java.awt.event.WindowEvent e) {
						System.exit(0);
					}
				});
				dialog.setVisible(true);
			}
	});
	}
 
	// Variables declaration - do not modify					 
	private javax.swing.JTextField CodeField;
	private javax.swing.JPanel Panel1;
	private javax.swing.JPanel Panel2;
	private javax.swing.JButton backButton;
	private javax.swing.JButton button;
	private javax.swing.JButton jButton1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JPanel mainPanel;
	// End of variables declaration				   

}





As you can see in the Atm in the buttonActionPerformed Event i create an array for accounts and checking what the user put inside the textfield to access his account.

private void buttonActionPerformed(java.awt.event.ActionEvent evt) {									   

  try{
   String accountNumber = CodeField.getText();
   int value = Integer.parseInt(accountNumber);
   
	 Bank  Bank = new  Bank();
		Bank.addAccount(new BankAccount(100, 1000, "Pireos"));
		Bank.addAccount(new BankAccount(200, 2000, "City"));
		Bank.addAccount(new BankAccount(300, 3000, "Pireos")); 
	   
   
 BankAccount a = Bank.find(value);			
 if (a == null){
   System.out.println("No Account with that number");
 }
 else{
	Panel1.setVisible(false);
	Panel2.setVisible(true);
	System.out.println("Account with number " + value + "  has balance " +
			 a.getBalance() + " The bank of this Account is " + a.getBank());
 }
 }catch(Exception ex){
	  JOptionPane.showMessageDialog(this,"Please type a number in the box");
  } 




So because i want to keep this array for the rest of the Application i want to put is somewhere else to be accessible from other events too and use it from there in every time event. I dont know if this is possible and even if it is whats the way.


Posted Image



Sorry for the big post.

This post has been edited by j0e: 23 July 2008 - 10:03 AM

Was This Post Helpful? 0
  • +
  • -

#5 lordms12  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 30
  • View blog
  • Posts: 339
  • Joined: 16-February 08

Re: Calling Arrays

Posted 23 July 2008 - 11:17 AM

Make it static anywhere and access it by ClassName.ARRAY_NAME
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Calling Arrays

Posted 23 July 2008 - 04:05 PM

Just make the Bank bank (not a good idea to have Bank Bank, I'll use Bank bank) variable an instance variable of ATM to anybody can see it and not only the ActionPerformed method

So as Bank bank is already defined you change your ActionPerformed to fill that "bank"

private void buttonActionPerformed(java.awt.event.ActionEvent evt) {									   

  try{
   String accountNumber = CodeField.getText();
   int value = Integer.parseInt(accountNumber);
   
	 bank = new  Bank();	 <-----------------------------
	 bank.addAccount(new BankAccount(100, 1000, "Pireos"));
	 bank.addAccount(new BankAccount(200, 2000, "City"));
	 bank.addAccount(new BankAccount(300, 3000, "Pireos")); 
	   
   
 BankAccount a = Bank.find(value);			
 if (a == null){
   System.out.println("No Account with that number");
 }
 else{
	Panel1.setVisible(false);
	Panel2.setVisible(true);
	System.out.println("Account with number " + value + "  has balance " +
			 a.getBalance() + " The bank of this Account is " + a.getBank());
 }
 }catch(Exception ex){
	  JOptionPane.showMessageDialog(this,"Please type a number in the box");
  } 




Now add in Bank a method to retreive the ArrayList

public ArrayList<BankAccount> getArray() {
   return Accounts;
}



Now in you vode you can retreive the ArrayList where you want as Bank bank is visible
ArrayList <BankAccount> list = bank.getArray();
Was This Post Helpful? 1
  • +
  • -

#7 j0e  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 46
  • Joined: 01-June 08

Re: Calling Arrays

Posted 24 July 2008 - 04:52 AM

View Postpbl, on 23 Jul, 2008 - 04:05 PM, said:

Just make the Bank bank (not a good idea to have Bank Bank, I'll use Bank bank) variable an instance variable of ATM to anybody can see it and not only the ActionPerformed method

So as Bank bank is already defined you change your ActionPerformed to fill that "bank"

private void buttonActionPerformed(java.awt.event.ActionEvent evt) {									   

  try{
   String accountNumber = CodeField.getText();
   int value = Integer.parseInt(accountNumber);
   
	 bank = new  Bank();	 <-----------------------------
	 bank.addAccount(new BankAccount(100, 1000, "Pireos"));
	 bank.addAccount(new BankAccount(200, 2000, "City"));
	 bank.addAccount(new BankAccount(300, 3000, "Pireos")); 
	   
   
 BankAccount a = Bank.find(value);			
 if (a == null){
   System.out.println("No Account with that number");
 }
 else{
	Panel1.setVisible(false);
	Panel2.setVisible(true);
	System.out.println("Account with number " + value + "  has balance " +
			 a.getBalance() + " The bank of this Account is " + a.getBank());
 }
 }catch(Exception ex){
	  JOptionPane.showMessageDialog(this,"Please type a number in the box");
  } 




Now add in Bank a method to retreive the ArrayList

public ArrayList<BankAccount> getArray() {
   return Accounts;
}



Now in you vode you can retreive the ArrayList where you want as Bank bank is visible
ArrayList <BankAccount> list = bank.getArray();



Wow!!!!After about 20 hours of headache it worked.Thanks a lot!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1