• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with add threads to array list  RSS feed

 
craigd gibson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi craig, Welcome to the Ranch!

For starters, you say:
Im having problems with my threads being passed through to my array list, any help at all would be greatly appreciated.

What problem are you having? It will be easier to help if you TellTheDetails (<- click that link).

Also, can you please edit your code and UseCodeTags (<- another link). It will make the code easier to read. I would do it for you, but it looks like the code is unformatted (not indentation) so I didn't want to possible mis-interpret the code. So could you add the code tags and make sure things are spaced/indented appropriately?

Thanks,
 
craigd gibson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steve thanks for the reply, The main problem I am having is I am unable to save Information to the array, in order for it to be sorted and searched through. My apologies for the poor layout, Hopefully this is it fixed now.






 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that isn't any better. First: you are doing too much on a line. Read this: DontWriteLongLines. Not only does it make the code hard to read on the forum, but it also usually means the code line is too complex to understand. If you need to, format the parameters of methods adding white space to make them readable. for example, I took the wage line and changed it to this:

See how much easier that is to read? I don't understand what it is doing because I don't know what BankAccount does, or what any of those Strings and booleans, and other things are supposed to be. But at least I know what values are parameters to what methods/constructors.

All that said, you are giving us non-functional snippets of code (still un-formatted, with no indentation so hard to read) which no context and no real description of the problem. You need to step back. Create a new example: what is called an SSCCE (<- link to click) that gives us just the bare minimum required to see your problem (no GUI, no extra distractions or unnecessary classes or methods, just the minimum to show the issue). Make sure it is complete, so we can copy it and compile it, and run it to see your issue without having to guess or fill in the blanks. Then you need to explain what your problem actually is. Saying 'I am unable to save information in an array' does not help. Which array? What makes you unable to do so? An error message? A man standing over your shoulder pressing the backspace key whenever you type [ or ]?
 
craigd gibson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
n
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
craigd gibson wrote:My apologies for the poor layout, Hopefully this is it fixed now.

All except one thing: please DontWriteLongLines; it makes your threads (and code) very hard to read. I've tried to break yours up, and I'd say that it probably makes your code easier to read.

And on that note, you might want to think about storing parameter values before you use them for clarity, rather than great strings of method invocations.

Winston
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!