java array question

2 views
Skip to first unread message

amiller

unread,
Sep 10, 2008, 2:45:25 PM9/10/08
to The Java Posse
Is this the right forum to ask a simple java question?

If not maybe someone could suggest a good Java forum and I'll be on my
way.

Question is regarding merging arrays.

Alan

Joshua Marinacci

unread,
Sep 10, 2008, 3:31:36 PM9/10/08
to java...@googlegroups.com
what is your question?

Robert O"Connor

unread,
Sep 10, 2008, 3:35:18 PM9/10/08
to java...@googlegroups.com
Easy: create an array that's double the size of the original array and
throw the data from the original array into the new one you just
created.

--rob

--
-Rob

Christian Catchpole

unread,
Sep 10, 2008, 4:01:13 PM9/10/08
to The Java Posse
Something like.. ource.length == 0 ? 1 makes sure it turns a zero
length array into one. else it will never expand

public static byte[] expandArray(byte[] source) {
byte[] result = new byte[source.length == 0 ? 1 :
source.length * 2];
System.arraycopy(source, 0, result, 0, sourceLen.length);
return result;
}

Christian Catchpole

unread,
Sep 10, 2008, 4:11:04 PM9/10/08
to The Java Posse
Ooops. I pasted in a byte[] copier. You can use reflection to
construct the same type of Object array that was passed in.. eg. You
pass in String[], you get a String[] back...

public static Object[] expandArray(Object[] source) {
Object[] result = (Object[])
Array.newInstance(source.getClass().getComponentType(),
source.length == 0 ? 1 : source.length * 2);
System.arraycopy(source, 0, result, 0, sourceLen.length);
return result;
}


String[] newone = (String[])expandArray( original );

I've got a whole bunch-o-stuff here..

http://code.google.com/p/catchpole/source/browse/trunk/src/java/net/catchpole/io/Arrays.java

Christian Catchpole

unread,
Sep 10, 2008, 4:13:29 PM9/10/08
to The Java Posse
And again with the typos.. I havn't compiled it..

public static Object[] expandArray(Object[] source) {
Object[] result =
(Object[])Array.newInstance(source.getClass().getComponentType(),
source.length == 0 ? 1 : source.length * 2);
System.arraycopy(source, 0, result, 0, source.length);
return result;
}

amiller

unread,
Sep 10, 2008, 4:12:20 PM9/10/08
to The Java Posse

I have an array of items that I need to merge together so that there
is only
one entry in the array per username (1st field) and the rest of the
numbers
are merged into the single entry.

I end up with this: but I want to have this:
============================= ==============================
usera 5 0 0 0 0 0 0 0 0 0 0 0 usera 5 2 0 0 0 0 0 0 0 0 0 0
userb 5 0 0 0 0 0 0 0 0 0 0 0 userb 5 1 3 0 0 0 0 0 0 0 0 0
userc 5 0 0 0 0 0 0 0 0 0 0 0 userc 5 4 3 0 0 0 0 0 0 0 0 0
usera 0 2 0 0 0 0 0 0 0 0 0 0
userb 0 1 0 0 0 0 0 0 0 0 0 0
userc 0 4 0 0 0 0 0 0 0 0 0 0
usera 0 0 0 0 0 0 0 0 0 0 0 0
userb 0 0 3 0 0 0 0 0 0 0 0 0
userc 0 0 3 0 0 0 0 0 0 0 0 0



Here are the details. Maybe I should be building the array
differently.

I have an array of "JobHist" objects. The JobHist class has a name
followed by 12 integers.
public JobHistory(String name,
int jan, int feb, int mar, int apr, int may,
int jun,
int jul, int aug, int sep, int oct, int nov,
int dec) {
.....
}

In my code I am doing something like:

for (int n = 1; n < 12; n++) {
// the month number is i
// gets some sql data from a db that returns some names and a
number
while (rs.next()) }
String username = rs.getString("username");
int jobcount = rs.getInt("jobcount");
switch (n) {
case 1: values.add(new
JobHistory(username,jobcount,0,0,0,0,0,0,0,0,0,0,0)); break;
case 2: values.add(new JobHistory(username,
0,jobcount,0,0,0,0,0,0,0,0,0,0)); break;
case 3: values.add(new JobHistory(username,
0,0,jobcount,0,0,0,0,0,0,0,0,0)); break;
case 4: values.add(new JobHistory(username,
0,0,0,jobcount,0,0,0,0,0,0,0,0)); break;
case 5: values.add(new JobHistory(username,
0,0,0,0,jobcount,0,0,0,0,0,0,0)); break;
case 6: values.add(new JobHistory(username,
0,0,0,0,0,jobcount,0,0,0,0,0,0)); break;
case 7: values.add(new JobHistory(username,
0,0,0,0,0,0,jobcount,0,0,0,0,0)); break;
case 8: values.add(new JobHistory(username,
0,0,0,0,0,0,0,jobcount,0,0,0,0)); break;
case 9: values.add(new JobHistory(username,
0,0,0,0,0,0,0,0,jobcount,0,0,0)); break;
case 10: values.add(new JobHistory(username,
0,0,0,0,0,0,0,0,0,jobcount,0,0)); break;
case 11: values.add(new JobHistory(username,
0,0,0,0,0,0,0,0,0,0,jobcount,0)); break;
case 12: values.add(new JobHistory(username,
0,0,0,0,0,0,0,0,0,0,0,jobcount)); break;

Wayne Fay

unread,
Sep 10, 2008, 4:33:14 PM9/10/08
to java...@googlegroups.com
No code, since this looks like a homework assignment, but rather a suggestion...

In your code, you have values.add(...). Perhaps you need to check if a
JobHistory involving that user has already been inserted, and if so,
simply adjust the existing entry in the values[] by adding the
jobcount to that entry. This may involve removing and re-adding the
JobHistory record.

Wayne

Christian Catchpole

unread,
Sep 10, 2008, 4:40:34 PM9/10/08
to The Java Posse
having 12 ints for the months of the year is not a very flexible
design. As you have found, you need 12 case statements to "hard code"
the conditions. Imagine now doing that with days of the year. As
Wayne says, perhaps a setJobcount(int month, int count) method.

amiller

unread,
Sep 10, 2008, 5:43:05 PM9/10/08
to The Java Posse
So how would I check the values ArrayList for having seen the user
before?
Would that look like this:

for ( int i = 0; i < values.size(); i++) {
if ( ((JobHistory)values[i]).name.compareTo(username) == 0 )
// seen before, set jobcount for month
setJobCount(i, n, jobcount);
} else {
// new entry
addNewUser(username, n, jobcount);
}
}

No this is't a homework assigmnet.
I'm not a programmer, just a unix admin trying to learn java.

Thanks

Alan

On Sep 10, 10:40 pm, Christian Catchpole <christ...@catchpole.net>
wrote:

Wayne Fay

unread,
Sep 10, 2008, 7:38:30 PM9/10/08
to java...@googlegroups.com
Well, I wouldn't use an ArrayList -- a HashMap is more appropriate.

With a HashMap, you get trivial access to the user's jobhistory via
get("userA") and put("userA", ...) methods.

Wayne

Michael Burgess

unread,
Sep 11, 2008, 12:03:42 AM9/11/08
to The Java Posse
Something like this maybe

ResultSet rs = null;
Map<String, List<Integer>> jobHistory = new HashMap<String,
List<Integer>>();
for (int n = 1; n < 13; n++) {
// the month number is i
// gets some sql data from a db that returns some names and a
number

while (rs.next()) {
String username = rs.getString("username");
int jobcount = rs.getInt("jobcount");

List<Integer> values = jobHistory.get(username);
if (values == null) {
values = new ArrayList<Integer>(13);
values.set(n, jobcount);
jobHistory.put(username, values);
} else {
values.set(n, jobcount);
}
}
}

Christian Catchpole

unread,
Sep 11, 2008, 12:23:56 AM9/11/08
to The Java Posse
On Sep 11, 2:03 pm, Michael Burgess <mburg...@dedata.com.au> wrote:
> values = new ArrayList<Integer>(13);
> values.set(n, jobcount);

Looks good. Only problem with using an ArrayList is that it only
expands when you add items to the end. Constructing with 13 is only a
suggested initial capacity.

new ArrayList(13).set(4, "");

produces...

Exception in thread "main" java.lang.IndexOutOfBoundsException:
Index: 4, Size: 0

I would recommend creating your own class which is added to the Map.
Within that have perhaps an int[12]; with setter, getter and add
methods which access the array safely.

Wayne Fay

unread,
Sep 11, 2008, 1:17:44 AM9/11/08
to java...@googlegroups.com
Since we're picking on you... ;-)

On Wed, Sep 10, 2008 at 9:03 PM, Michael Burgess <mbur...@dedata.com.au> wrote:
> } else {
> values.set(n, jobcount);
jobHistory.put(username, values);

(I'm only sending this because I'm concerned Alan may use some of this
as-is, and get annoyed when his values don't update properly...)

Wayne

amiller

unread,
Sep 11, 2008, 2:27:55 AM9/11/08
to The Java Posse
Christian,

would I create my JobHistory class like this:

public JobHistory(String name, int[] months) {
this.name = name;
this.months = months;
}
public int getMonthData(int mon) {
return months[mon];
}
public void setMonthData(int mon, int data) {
this.months[mon] = data;
}



On Sep 11, 7:17 am, "Wayne Fay" <wayne...@gmail.com> wrote:
> Since we're picking on you... ;-)
>

Christian Catchpole

unread,
Sep 11, 2008, 3:41:17 AM9/11/08
to The Java Posse
yes, i was thinking that, if you don't need to track more than 1
months data per object, this is the way to go.

amiller

unread,
Sep 11, 2008, 4:43:38 AM9/11/08
to The Java Posse
I tried the public JobHistory(String name, int[] months) {...}
together with the HashMaps but couldn't get it to work.

What I do have something is this.

while (rs.next()) {
String username = rs.getString("username");
int jobcount = rs.getInt("jobcount");

int found = 0;
for (int v = 0; v< values.size(); v++) {
JobHistory jh = values.get(v);
if (jh.name.compareTo(username) == 0) {
found = 1;
jh.setMonthData(n, jobcount);
values.set(v, jh);
}
}
if ( found == 0) {
values.add( new JobHistory(username,n,jobcount));
}
}

And in my JobHistory class I have added:

a constructor:
public JobHistory(String username, int mon, int jobcount) {
// which is a 12-item switch/case statement
}
a method:
public void setMonthData(int mon, int jobcount) {
// ALSO a 12-item switch/case statement
}

Alan

On Sep 11, 9:41 am, Christian Catchpole <christ...@catchpole.net>
wrote:

sherod

unread,
Sep 11, 2008, 7:32:44 AM9/11/08
to The Java Posse
A suggestion, perhaps this problem could probably be much easily
solved in straight SQL.

select username, month_number, sum(jobcount) from jobs group by
username, month_number

That will get you the sum of job counts by username and month.

Then its a matter of turning the list of results into a table for
output.

It's probably best if you post the structure (DDL / SQL) of the
underlying database table so specific code could be written.

phil.s...@gmail.com

unread,
Sep 12, 2008, 4:15:31 PM9/12/08
to The Java Posse
and people say Java is verbose.... don't know why ;)

On Sep 10, 2:13 pm, Christian Catchpole <christ...@catchpole.net>
wrote:
Reply all
Reply to author
Forward
0 new messages