Results 1 to 4 of 4
  1. #1
    Craig is offline Member
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Default Help with add threads to array list

    Hi, Im new to this forum so please bare with me. Im having problems with my threads being passed through to my array list, any help at all would be greatly appreciated. Here are snippets of my code.

    public class ArrayList<T> {

    private int arrSize = 10;
    private int i;
    private T[] arr;
    public int length;
    public int count;

    public ArrayList(){
    count = 0;
    arr = (T[]) (new Object[10]);
    }

    public void add(T element){

    arr[i] = element;
    length = i;
    i++;
    }

    public T get(int Ele){
    T res = arr[Ele];

    return res;
    }

    }


    public Bank()
    {
    accountBalance = 0;
    }

    public synchronized void deposit(int addAmount, String name)
    {
    accountBalance+=addAmount ;
    System.out.println(name + " added " + addAmount) ;
    System.out.println("Account balance is now " + accountBalance);
    liststore(name, String.valueOf(addAmount),"date","Incoming");
    }

    public synchronized void withdraw(int takeAmount, String name)
    {
    accountBalance-=takeAmount ;
    System.out.println(name + " took " + takeAmount) ;
    System.out.println("Account balance is now " + accountBalance);
    liststore(name, String.valueOf(takeAmount),"date","Incoming");
    }

    public synchronized int getBalance()
    {
    return accountBalance ;
    }

    public void liststore(String type, String amount, String date, String transType)
    {
    String[] d1 = {type,amount,date,transType};

    ArrayList<String[]> AList = new ArrayList<>();
    String[] a1 = {"wage"};
    AList.add(a1);
    String[] c4 = AList.get(0);
    System.out.println(d1);
    }

    } // End of class BankAccount


    ArrayList<String> AList = new ArrayList<String>();
    // String[] a1 = {"wage"};
    AList.add(wage.toString());
    //AList.add(incomefield2.getText().toString());


    //String[] c4 = AList.get(0);

    System.out.println(AList.get(0));
    // System.out.println(AList.get(1));
    }
    This piece of code is in my frame class, that is building the GUI.

    Thanks for the help, my apologies for the messy layout.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,783
    Rep Power
    5

    Default Re: Help with add threads to array list

    First, public class ArrayList<T> is probably not a good idea. Even though it is a different package there is already a class by that name in the JDK. That may be part of your problem. Second, I don't see any threads.

    Also, please correct the indentation and place your code between [code][/code] tags.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Craig is offline Member
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Default Re: Help with add threads to array list

    Java Code:
    public void actionPerformed(ActionEvent e) 
        {
             bank = new Bank();
             
             wage = new Thread(new BankAccount(incomefield.getText().toString(), Integer.parseInt(incomefield2.getText().toString()), bank, true,Integer.parseInt(incomefield3.getText().toString())));
             interest = new Thread(new BankAccount(incomefield4.getText().toString(), Integer.parseInt(incomefield5.getText().toString()), bank, true,Integer.parseInt(incomefield6.getText().toString())));
             food = new Thread(new BankAccount(outgo1.getText().toString(),Integer.parseInt(outgo2.getText().toString()), bank, false,Integer.parseInt(outgo3.getText().toString())));
             electricity = new Thread(new BankAccount(outgo4.getText().toString(),Integer.parseInt(outgo5.getText().toString()), bank, false, Integer.parseInt(outgo6.getText().toString())));
             entertainment = new Thread(new BankAccount(outgo7.getText().toString(),Integer.parseInt(outgo8.getText().toString()), bank, false,Integer.parseInt(outgo9.getText().toString())));
             oil = new Thread(new BankAccount(outgo10.getText().toString(),Integer.parseInt(outgo11.getText().toString()), bank, false, Integer.parseInt(outgo12.getText().toString())));
             petrol = new Thread(new BankAccount(outgo13.getText().toString(),Integer.parseInt(outgo14.getText().toString()), bank, false, Integer.parseInt(outgo15.getText().toString())));
    
            starttime();
            
            wage.start();
            food.start();
            electricity.start();
             interest.start();
            entertainment.start();
            oil.start();
            petrol.start();
            
            ArrayList<String> AList = new ArrayList<String>();
    [code]
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package bankaccount;

    /**
    *
    * @author Craig
    */
    public class ArrayList<T> {

    private int arrSize = 10;
    private int i;
    private T[] arr;
    public int length;
    public int count;

    public ArrayList(){
    count = 0;
    arr = (T[]) (new Object[10]);
    }

    public void add(T element){

    arr[i] = element;
    length = i;
    i++;
    }

    public T get(int Ele){
    T res = arr[Ele];

    return res;
    }

    }

    [code/]

    [code]
    public Bank()
    {
    accountBalance = 0;
    }

    public synchronized void deposit(int addAmount, String name)
    {
    accountBalance+=addAmount ;
    System.out.println(name + " added " + addAmount) ;
    System.out.println("Account balance is now " + accountBalance);
    liststore(name, String.valueOf(addAmount),"date","Incoming");
    }

    public synchronized void withdraw(int takeAmount, String name)
    {
    accountBalance-=takeAmount ;
    System.out.println(name + " took " + takeAmount) ;
    System.out.println("Account balance is now " + accountBalance);
    liststore(name, String.valueOf(takeAmount),"date","Incoming");
    }

    public synchronized int getBalance()
    {
    return accountBalance ;
    }

    public void liststore(String type, String amount, String date, String transType)
    {
    String[] d1 = {type,amount,date,transType};

    ArrayList<String[]> AList = new ArrayList<>();
    String[] a1 = {"wage"};
    AList.add(a1);
    // AList.add(d1);

    String[] c4 = AList.get(0);

    // System.out.println(c4);

    System.out.println(d1);
    //System.out.println(AList.toString(d1));


    }

    } // End of class BankAccount
    [code/]

    [code]
    public class BankAccount implements Runnable
    {
    Boolean transaction;

    String type;
    int money;
    int freq;
    Bank sim;
    Timer timer;

    public BankAccount(String name, int amount, Bank account, boolean trans, int frequency)
    {
    type = name;
    money = amount;
    sim = account;
    transaction = trans;
    freq = (int)(frequency *1000);



    }//end of newTimer class
    @Override
    public void run()
    {
    try
    {


    while(true)
    {

    if(transaction == true)
    {
    sim.deposit(money, type);
    }

    else
    {
    sim.withdraw(money, type);
    }

    Thread.sleep(freq);
    }
    }

    catch (InterruptedException ex)
    {

    }
    }

    }//end of class
    [code/]

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,783
    Rep Power
    5

    Default Re: Help with add threads to array list

    Your end code tag is wrong (forward slash comes first). Also, where does incomefield come from? What is required is a small compilable working example that demonstrates the problem you are experiencing. I would make a guess (perhaps incorrectly) that a method which is named getText() already returns a String. So calling toString() just clutters the code.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Shared array between Threads?
    By driiper in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-16-2012, 09:58 AM
  2. Linked List, Array List time complexity
    By Rick99771977 in forum New To Java
    Replies: 4
    Last Post: 08-18-2011, 05:37 AM
  3. Adding elements in array list and vectors using threads
    By hina.yousuf@seecs.edu.pk in forum Advanced Java
    Replies: 2
    Last Post: 10-10-2010, 03:07 AM
  4. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  5. How an array of Threads works...
    By Moncleared in forum New To Java
    Replies: 6
    Last Post: 09-09-2009, 01:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •