Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort, remove objects in a arraylist?

Status
Not open for further replies.

javaken

Technical User
May 15, 2004
20
BE
Suppose:
You have following arraylist-output:
4 AAAAAA
5 ZZZZZZ
2 DDDDDD
1 AAAAAA
3 AAAAAA

one row is one object:

Questions:
how to sort rows on numbers ascending?
==> output = 1 AAAAAA
2 DDDDDD
3 AAAAAA
4 AAAAAA
5 ZZZZZZ
how to sort column 2?
==> output = 1 AAAAAA
3 AAAAAA
4 AAAAAA
2 DDDDDD
5 ZZZZZZ

suppose you want to remove the rows where data = AAAAAA
==> output after this is:
2 DDDDDD
5 ZZZZZZ

I don't understand to do this in a simple way!!!!
Is het possible to give some code concerning this???
Thank you very much!!!

Greetings from a relative new user ...... [pipe]

existing code:

*
* Created on 21-mei-2004
*
*/


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;


public class ReadingFile {
public ReadingFile(String tempFile) throws IOException{

File f = new File(tempFile);
ArrayList al = new ArrayList();
BufferedReader br = new BufferedReader(new FileReader(f));
String line = "";
while ((line = br.readLine()) != null) {
al.add(line);
}

Object[] array = al.toArray();

System.out.println("Object:");
for (int count = 0; count < array.length; count++)
System.out.println(array[count]);

}

public static void main(String args[]) throws IOException
{
ReadingFile rfObj = new ReadingFile("objects.txt");
}

}
 
Hi javaken,

Maybe this code will point you in the right direction :

Code:
		Object[] array = new Object[]{"ddddd", "bbbb", "aaaaa", "ccccc"};

		System.err.println("Before :");
		for (int i = 0; i < array.length; i++) {
			System.err.println(array[i]);
		}

		// Arrays class is in java.util
		Arrays.sort(array);

		System.err.println("\nAfter :");
		for (int i = 0; i < array.length; i++) {
			System.err.println(array[i]);
		}
 
i prefer vector to array for the operation of element removing.
 
Suppose you have a class X:
class X produce an object exists of 2 elements:
ID + Contents

==> each object is formed by this 2 elements ...
==> in code you have 2 classes ...

You have following arraylist-output:
4 AAAAAA 4 = ID AAAAAA = element Contents
5 ZZZZZZ
2 DDDDDD
1 AAAAAA
3 AAAAAA

one row is one object:

Questions:
how to sort rows on numbers ascending?
==> output = 1 AAAAAA
2 DDDDDD
3 AAAAAA
4 AAAAAA
5 ZZZZZZ
how to sort column 2?
==> output = 1 AAAAAA
3 AAAAAA
4 AAAAAA
2 DDDDDD
5 ZZZZZZ

suppose you want to remove the rows where data = AAAAAA
==> output after this is:
2 DDDDDD
5 ZZZZZZ

How to do this? Is it possible with your code?

Thank you!!!! [smile2]


 
It's very simple. Make a class:

class Object
{
int id; // e.g. 4
data YourData; // e.g. AAAAAAA
}


Generate your list, or array, or vector or whatever.

initialize(Object[]);

sort(Object[],Object[].id); // sort based on id


The key is to work with objects.
 
Hey,

Who can give a simplified version of code to do this?

Sorry, but I have lost the key .... °) or in other words I have problems to do this ....

Thanks [upsidedown]




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top