Collecting elements from array

Hi,

Is it possible in Java? I have a String[] source array and I have to collect some items from it to an another (new) String[] so, that I should go through on the source array only one time.

E.g.

String[] source = {"foo1", "","","foo2","","foo3","foo4",""}

result array should be: {"foo1","foo2","foo3","foo4"}

The problem is that we do not know the size of the result array in advance, just after reading through the source array.

- Cast cannot be used for the result, result must be a type of String[].

- Java version: 1.3.1.

In other programming languages this is an easy task, because we can use dynamic array.

This was my first solution, but it creates an Object[], which cannot be cast to String[]:

String[] names ={"foo1","","","foo2","foo3","","foo4",""};

Vector coll_names =new Vector();

for (int i = 0; i < names.length; i++){

if (names[i] !=""){

coll_names.add(names[i]);

}

}

Object[] coll_names_arr = coll_names.toArray();

Thank you!

Akos.

[1603 byte] By [TAkosa] at [2007-11-27 10:35:46]
# 1

if you try toString() it may.

Adi1000a at 2007-7-28 18:37:00 > top of Java-index,Java Essentials,Java Programming...
# 2

i mean convert object to string.What happens.In other case

try to use String.valueof() in succession.

Adi1000a at 2007-7-28 18:37:00 > top of Java-index,Java Essentials,Java Programming...
# 3

Hello,

You have to do it like this:

import java.util.Vector;

public class Print {

public static void main(String[] args) {

String[] names = {"foo1", "", "", "foo2", "foo3", "", "foo4", ""};

Vector coll_names = new Vector();

for (int i = 0; i < names.length; i++) {

if (names[i] != "") {

coll_names.add(names[i]);

}

}

String[] coll_names_arr = (String []) coll_names.toArray(new String[coll_names.size()]);

for (int i = 0; i < coll_names_arr.length; i++) {

System.out.println(coll_names_arr[i]);

}

}

}

_helloWorld_a at 2007-7-28 18:37:00 > top of Java-index,Java Essentials,Java Programming...
# 4

Hi,

Thank you really much this is perfect!

Akos.

TAkosa at 2007-7-28 18:37:00 > top of Java-index,Java Essentials,Java Programming...
# 5

> Hi,

>

> Thank you really much this is perfect!

>

> Akos.

NO it's not. Try it again with this array:

String[] names = {"foo1", "", new String(""), "foo2", "foo3", "", "foo4", ""};

prometheuzza at 2007-7-28 18:37:00 > top of Java-index,Java Essentials,Java Programming...
# 6

Yes, I should use the equals method for comparison, thank you!

(e.g. if (!"".equals(names[i])) {...}

)

TAkosa at 2007-7-28 18:37:00 > top of Java-index,Java Essentials,Java Programming...
# 7

> Yes, I should use the equals method for comparison,

> thank you!

>

> (e.g. if (!"".equals(names[i])) {...})

Correct. And you even took care of the situation when your array contains null references by first providing an empty String: well done!

prometheuzza at 2007-7-28 18:37:00 > top of Java-index,Java Essentials,Java Programming...
# 8

Hi,

For a second look, in the Java source we can see, that the Vector calls arraycopy in this case. So I think that it reads through the array again. :( It is a native method but it still reads and copies the the whole array again, I think.

Source of the Vector class:

public synchronized <T> T[] toArray(T[] a) {

if (a.length < elementCount)

return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass());

System.arraycopy(elementData, 0, a, 0, elementCount);

if (a.length > elementCount)

a[elementCount] = null;

return a;

}

Akos.

TAkosa at 2007-7-28 18:37:00 > top of Java-index,Java Essentials,Java Programming...
# 9

Thank you prometheuzz! :)

TAkosa at 2007-7-28 18:37:00 > top of Java-index,Java Essentials,Java Programming...
# 10

> Hi,

>

> For a second look, in the Java source we can see,

> that the Vector calls arraycopy in this case. So I

> think that it reads through the array again. :( It

> is a native method but it still reads and copies the

> the whole array again, I think.

Yes, but why is that a problem?

prometheuzza at 2007-7-28 18:37:00 > top of Java-index,Java Essentials,Java Programming...
# 11

> Yes, but why is that a problem?

Because I wanted to find a solution that reads through the source array only once. Thus If we have much more elements it would not be much slower. So, reading the whole array through again means a significant performance loss.

TAkosa at 2007-7-28 18:37:00 > top of Java-index,Java Essentials,Java Programming...
# 12

> > Yes, but why is that a problem?

>

> Because I wanted to find a solution that reads

> through the source array only once. Thus If we have

> much more elements it would not be much slower. So,

> reading the whole array through again means a

> significant performance loss.

I highly doubt that. Did you perform tests that showed you had performance because of this?

prometheuzza at 2007-7-28 18:37:00 > top of Java-index,Java Essentials,Java Programming...
# 13

> Did you perform tests that showed you had performance because of this?

No, but my assumption is that reading through an array twice is slower than reading only one time. I do not know what arraycopy does exactly, but if it copies the array than I think that it have to read (move) all the elements again.

In other languages there is a possibility to create a dynamic array and just truncate it after we collected the elements. This means only one read-throught. I hoped that a similar solution exists in Java too.

TAkosa at 2007-7-28 18:37:00 > top of Java-index,Java Essentials,Java Programming...
# 14

have you heard of the term "trivial performance gains"? Don't waste non-trivial programmer-hours just for sake of the above. Put lots of effort into performance gains when you KNOW you will get performance gains.

petes1234a at 2007-7-28 18:37:00 > top of Java-index,Java Essentials,Java Programming...
# 15

> > Did you perform tests that showed you had

> > performance because of this?

>

> No, but my assumption is that reading through an

> array twice is slower than reading only one time. I

> do not know what arraycopy does exactly, but if it

> copies the array than I think that it have to read

> (move) all the elements again.

I merely copies the references to those objects, which is no big deal.

> In other languages there is a possibility to create a

> dynamic array and just truncate it after we collected

> the elements. This means only one read-throught. I

> hoped that a similar solution exists in Java too.

If you're worried about performance, you should leave that old Vector and use an ArrayList instead. And don't worry about that toArray() method.

prometheuzza at 2007-7-28 18:37:04 > top of Java-index,Java Essentials,Java Programming...
# 16

> have you heard of the term "trivial performance

> gains"? Don't waste non-trivial programmer-hours

> just for sake of the above. Put lots of effort into

> performance gains when you KNOW you will get

> performance gains.

I agree with you. I just was wondering how can I write at least nearly as effective code in Java as in other programming languages. You know, what if I really have performance issues? (We are using Java on a telecommunication back-end hardware)

TAkosa at 2007-7-28 18:37:04 > top of Java-index,Java Essentials,Java Programming...
# 17

> I agree with you. I just was wondering how can I

> write at least nearly as effective code in Java as in

> other programming languages.

Well, not with micro-optimization.

> You know, what if I really have performance issues? (We are using Java on a telecommunication back-end hardware)

A person saying "what if I really have performance issues" is the wrong person to try to solve performance issues.

Too often junior programmers think they know enough about Java to "optimize", when they have no idea of the inner workings of Java.

If you already have your input as an ArrayList and don't need it as an array, you're well off.

If you need to convert it from array to ArrayList and back again, you're gonna need to do some copying.

That copying has no effect on whether Java is as effective as other languages, since it's a micro-optimization.

-Kayaman-a at 2007-7-28 18:37:04 > top of Java-index,Java Essentials,Java Programming...
# 18

Yes, it is a micro-optimization. But this particular one is possible in other languages. I just wanted to find out whether it is so in Java. I also wanted to get to know the possibilities in Java according to these kind of problems.

We often can modify only a little part of the system code, and it is often good to consider these kind of performance issues or else if we have an "ineffective" addition here, than there, and so on at the end the whole system might slow down. And yes, then we can force a macro-optimization on the management. :)

Well, I agree that it is not always so important.

Thank you.

TAkosa at 2007-7-28 18:37:04 > top of Java-index,Java Essentials,Java Programming...