We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Sort elements of an ArrayList
Page Index Toggle Pages: 1
Sort elements of an ArrayList (Read 387 times)
Sort elements of an ArrayList
Mar 1st, 2009, 1:19am
 
Hello,
when entering numbers an pressing Enter, the numbers are added to my ArrayList.
I now want to sort them by size. So while entering new numbers it constantly updates and one always gets the current series of numbers sorted by size.
I want to get the elements of the ArrayList printed. So that one can see what sort of numbers are already entered. (I thought of using println but that does not work yet). Does anyone has an idea how to get the numbers printed out or if there is any hard mistake in my code?
Thanks so far!!!

Code:

ArrayList numbers;
int count = 1;

void setup() {
size (100,100);
numbers = new ArrayList ();
}

void draw () {
}

class Number implements Comparable{
StringBuffer buffer_width;
String string_width;
int int_width;
Number () {
buffer_width = new StringBuffer();
}
void display() {
buffer_width.append(key);
string_width = buffer_width.toString();
int_width = int(string_width);
println (numbers);
}
void report() {
println (int_width);
}
public int compareTo (Object o) {
Number n = (Number) o;
return (int) (int_width);
}
}

void keyReleased () {
if (keyCode == ENTER) {
count ++;
}
if (count == 1) {
for (int i = 0; i<numbers.size(); i++) {
Number n = (Number) numbers.get (i);
n.display();
n.report();
Collections.sort(numbers);
n.report();
}
count = 0;
numbers.add(new Number ());
}
}







Re: Sort elements of an ArrayList
Reply #1 - Mar 1st, 2009, 1:56am
 
You've defined the Number class, but have not provided a toString() method. So, when each Number object is printed, the Object.toString() method is used. The Object.toString() prints essentially an object ID. It's the class name an the hashcode.

So, a partial fix to your code is to add a toString() method to the definition of the Number class. For example,

 public String toString() {
   return String.valueOf(int_width);
 }

But, I don't see any place in your code where the value of the Number is being stored. In the toString() method I provided above, I assumed that the int_width field was supposed to contain the value.
Re: Sort elements of an ArrayList
Reply #2 - Mar 1st, 2009, 10:52am
 
WombatNation is right, you are missing a toString(). But also a proper compareTo, adding keys to your buffer and some other problems.. :-D

Your base idea isn't bad, although a bit too complex (no real need to a buffer per Number, you can make an ArrayList of Integers and manage the buffer at global level).

Anyway, for educational purpose, I corrected and finished along your idea:
Code:
ArrayList numbers;

void setup() {
noLoop();
numbers = new ArrayList();
numbers.add(new Number());
}

void draw () {
}

class Number implements Comparable {
StringBuilder buffer;
Number() {
buffer = new StringBuilder();
}
void update(char c) {
// Might check c before adding it
buffer.append(c);
}
int getValue() {
return Integer.valueOf(buffer.toString());
}
String toString() {
return buffer.toString();
}
public int compareTo(Object o) {
Number n = (Number) o;
int i1 = getValue();
int i2 = n.getValue();
return i1 == i2 ? 0 : (i1 > i2 ? 1 : -1);
}
}

void keyReleased() {
boolean bDisplay = false;
if (keyCode == ENTER) {
bDisplay = true;
println("");
} else {
// Update latest number
Number n = (Number) numbers.get(numbers.size() - 1);
n.update(key);
print(key);
}
if (bDisplay) {
Collections.sort(numbers);
println(numbers);
numbers.add(new Number());
}
}

Last note: Java has also Collections that maintain order on each insertion. Not really useful here where ArrayList is always short and sorting done only on user input, but good to know.
Re: Sort elements of an ArrayList
Reply #3 - Mar 1st, 2009, 4:11pm
 
Wow, Phil thanks for your time....

I worked through your code and tried to modify it again. I can now enter and sort my integer-values, and I now want them to be displayed as rectangles. So every time you type in a value, a rectangle is drawn on the screen and its width depends on the current value you've just entered.

Problem: I use this for-loop, but it does not work yet, as I have only int-values in my rect function and numbers.get(i) is an object....

Code:

for (int i = 0; i<numbers.size(); i++) {
fill (0);
rect (i*20,10, numbers.get(i) ,5);
}


I wrote this in the draw() function so that it is constantly updated.

So all I need to do is to get numbers.get(i) into an int as well...as this is still an Object I cannot draw my rectangles. Any idea how this might work?

I've googled to find a way to convert it like using "parseInt" but that does not work....

thanks a lot for your time and energy
Re: Sort elements of an ArrayList
Reply #4 - Mar 2nd, 2009, 11:30am
 
Code:
for (int i = 0; i < numbers.size(); i++) {
fill(0);
Number n = (Number) numbers.get(i); // You have to cast back objects from a Collection
rect(i*20, 10, n.getValue(), 5);
}
Re: Sort elements of an ArrayList
Reply #5 - Mar 2nd, 2009, 12:41pm
 
Ok that's what I was looking for.

I now put it all together like this.

Code:

ArrayList numbers;
int o1;
void setup() {
size (400,400);
numbers = new ArrayList();
numbers.add(new Number());
}

void draw () {
for (int i = 0; i < numbers.size(); i++) {
fill(0);
Number n = (Number) numbers.get(i); // You have to cast back objects from a Collection
rect(i*20, 10, n.getValue(), 5);
}
}

class Number implements Comparable {
StringBuilder buffer;
Number() {
buffer = new StringBuilder();
}
void update(char c) {
// Might check c before adding it
buffer.append(c);
}
int getValue() {
return Integer.valueOf(buffer.toString());
}
String toString() {
return buffer.toString();
}
public int compareTo(Object o) {
Number n = (Number) o;
int i1 = getValue();
int i2 = n.getValue();
return i1 == i2 ? 0 : (i1 > i2 ? 1 : -1);
}
}

void keyReleased() {
boolean bDisplay = false;
if (keyCode == ENTER) {

bDisplay = true;
println("");
}
else {
// Update latest number
Number n = (Number) numbers.get(numbers.size() - 1);
n.update(key);
print(key);
}
if (bDisplay) {
Collections.sort(numbers);
println(numbers);
numbers.add(new Number());
}
}



It gives me an error when executing the script.
Code:

int getValue() {
return Integer.valueOf(buffer.toString());
}


The error is named as: NumberFormatException: For input string ""

I think it is because when starting the script there are no values yet to use. So  I tried to make a counter so that the for-loop is only executed when the mouse is released once. That works so far as if I don't get an error right from the beginning. But as soon as I typed in some values and clicked the mouse the same error occurs....

any idea what might be still wrong?
Re: Sort elements of an ArrayList
Reply #6 - Mar 2nd, 2009, 1:35pm
 
You have to add checks in your code: I coded to the bare bones, but as my comment points out, you should check user types only digits (and reject silently anything else) and check buffer before using it.
Eg.:
Code:
int getValue() {  
 if (buffer.length() == 0)
   return 0;
 return Integer.valueOf(buffer.toString());
}
Re: Sort elements of an ArrayList
Reply #7 - Mar 6th, 2009, 2:13pm
 
Ok this is my code so far....
When typing in a number you can see the the typed number as the width of the current rect.
Let's say you type in these numbers 10, 20, 30. Then you can see 3 rects right underneath each other. But when you now type in 25 this 4th rect should be 5 pixels smaller than the 3rd rect which is 30 pixels wide. But as soon as you type in a smaller number th rect is automatically as long as the highest number.

I dont get why because before you press Enter the correct size is shown in the window.
Because what I finally want to do is, that you can first of all type in many number and all rects have a different size. And when you Click the mouse the screen is deleted and the rects (and also the numbers) are sorted by size....
I hope you get what I mean.... :/ Can you help me again? I think we're close to solve the whole problem......thanks a lot

I thought that the for-loop inside the draw()-Function only cares for the last typed-in number....

Code:

ArrayList numbers;
void setup() {
size (400,400);
numbers = new ArrayList();
numbers.add(new Number());
}

void draw () {
for (int i = 0; i < numbers.size(); i++) {
fill(0);
Number n = (Number) numbers.get(i); // You have to cast back objects from a Collection
rect(20, i*10, n.getValue(), 5);
}
}

class Number implements Comparable {
StringBuilder buffer;
Number() {
buffer = new StringBuilder();
}
void update(char c) {
// Might check c before adding it
buffer.append(c);
}
int getValue() {
if (buffer.length() == 0)
return 0;
return Integer.valueOf(buffer.toString());
}
String toString() {
return buffer.toString();
}
public int compareTo(Object o) {
Number n = (Number) o;
int i1 = getValue();
int i2 = n.getValue();
return i1 == i2 ? 0 : (i1 > i2 ? 1 : -1);
}
}

void keyReleased() {
boolean bDisplay = false;
if (keyCode == ENTER) {

bDisplay = true;
println("");
}
else {
// Update latest number
Number n = (Number) numbers.get(numbers.size() - 1);
n.update(key);
print(key);
}
if (bDisplay) {
Collections.sort(numbers);
println(numbers);
numbers.add(new Number());
}
}



Re: Sort elements of an ArrayList
Reply #8 - Mar 6th, 2009, 4:55pm
 
That's just the classical "I forgot to call background() in draw()" syndrome... :-D

Don't forget to reject non-digit chars.
Re: Sort elements of an ArrayList
Reply #9 - Mar 6th, 2009, 6:51pm
 
ooooppppsss - i never thought that this might happen to me one day Wink
Ok, i reject non-digit chars by this if...
don't know if there is an easier way to do so......

Code:

void keyReleased() {
if (key == 0 || key == 1 || key == 2 || key == 3 || key == 4 || key == 5 || key == 6 || key == 7 || key == 8 || key == 9){

**** Code is here ****
}
}

Re: Sort elements of an ArrayList
Reply #10 - Mar 7th, 2009, 9:29am
 
keys are characters, so you must check with:

if (key >= '0' && key <= '9')
Page Index Toggle Pages: 1