# Referencing Two set of arrays with different data type String and Numb

Page 1 of 1

## 13 Replies - 2181 Views - Last Post: 08 January 2010 - 07:01 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=148586&amp;s=2fa95cb5d7b55af1a1fbc9e860ae4333&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 larrybalz4life

Reputation: 0
• Posts: 7
• Joined: 06-January 10

# Referencing Two set of arrays with different data type String and Numb

Posted 06 January 2010 - 09:06 PM

I have two set of arrays one set is Double [] data type and the the other is String and are all 12 values each. The problem i have is to reference/compare/ check.
For example if two set of arrays
array a is {1.1, 1.2, 1.3}
array b is {aa,ab,ac}
If i use a formula to find the highest value of a which in this case is 1.3, print the String value in array b that references highest value in array a in this case is ac but wouldn't be easy to find out if you dealing with huge amount of data,

Is This A Good Question/Topic? 0

## Replies To: Referencing Two set of arrays with different data type String and Numb

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12278
• Posts: 45,364
• Joined: 27-December 08

## Re: Referencing Two set of arrays with different data type String and Numb

Posted 06 January 2010 - 09:23 PM

Since the arrays are parallel, just return the highest index in the double[], which will be the corresponding index in the String[]. So 1.3 is located at index 2, so if the findMax portion of your code returns 2, you can access a[2] to get 1.3 and b[2] to get "ac". Also, remember that String literals are enclosed between double quotes. If you don't have to double quotes around the elements in your String[], then Java will look for a variable named aa, ab, ac, etc. and crash when it doesn't find these variables.

### #3 g00se

• D.I.C Lover

Reputation: 3536
• Posts: 16,028
• Joined: 20-September 08

## Re: Referencing Two set of arrays with different data type String and Numb

Posted 07 January 2010 - 12:54 AM

Sounds like you have a design problem: if you have a set of data that is connected to another set of data, they should be connected in the design of your program too. Your problem then disappears

### #4 larrybalz4life

Reputation: 0
• Posts: 7
• Joined: 06-January 10

## Re: Referencing Two set of arrays with different data type String and Numb

Posted 07 January 2010 - 05:09 PM

WOW.. thanks guys.. For the quick response..
@macosxnerd101..
I do not quite understand your explanation.
Please i have added my code. I know the maximum value of the month but how do i check in the months (string) with stock (double) that corresponds with the highest value and print the month out.

```class calcStock

{
public static void main(String args[])
{
double [] p;

p = new double[12];
p[0]=4.5;p[1]=4.63;p[2]=3.65;p[3]=4.70;p[4]=4.72;
p[5]=4.21;p[6]=5.02;p[7]=4.81;p[8]=4.10;p[9]=4.11;
p[10]=4.09;p[11]=4.01;

String [] m;
m = new String [12];
m[0]="January";m[1]="Feburary";m[2]="March";
m[3]="April"; m[4]="May";m[5]="June";m[6]="July";
m[7]="Auguest";m[8]="September";m[9]="October";
m[10]="November";m[11]="December";

double max=p[0]; // Initialise maximum to first value
for (int i=1; i<p.length; i++)
{
if (p[i] > max)
{
max = p[i];   // new maximum
}
}

double min=p[0]; // Initialise minimum to first value
for (int i=1; i<p.length; i++)
{
if (p[i] < min)
{
min = p[i];   // new minimum
}
}
p.equals(m);

System.out.println("The Worst month value is= " + min +m);
System.out.println("The Best month value is= " + max+m);
}
}

```

### #5 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Referencing Two set of arrays with different data type String and Numb

Posted 07 January 2010 - 05:48 PM

```			double [] p = {4.5, 4.63, 3.65, ...., 4.09, 4.01};
String [] m = {"January", "February", ...., "November", "December"};

```

would do the job a lot clearer

Now you want to write code in Java ... Java is an OO language so instead of comparing to arrays you should create an object that contains your pair of data

```class CalcStock {
private double p;
private String m;

// constructor
CalcStock(double price, String month) {
p = price;
m = month;
}

public static void main(String[] args) {
double [] p = {4.5, 4.63, 3.65, ...., 4.09, 4.01};
String [] m = {"January", "February", ...., "November", "December"};

// create the 12 CalcStock object
CalcStock[] cs = new CalcStock[12];
for(int i = 0; i < 12; i++)
cs[i] = new CalcStock(p[i], m[i]);

// now you can do your loops
double max = cs[0].p;
int maxIndex = 0;
for(int i = 1; i < 12; i++) {
if(cs[i].p > max) {
max = cs[i].p;
maxIndex = i;
}
}
// ok now cs[maxIndex] is the biggest month and its name is cs[maxIndex].m
}
}

```

Or event better
```class CalcStock {
private double p;
private String m;

// constructor
CalcStock(double price, String month) {
p = price;
m = month;
}
// returns the max of two CalcStock
CalcStock getMax(CalcStock other) {
if(p > other.p)
return this;
else
return other;
}

public static void main(String[] args) {
double [] p = {4.5, 4.63, 3.65, ...., 4.09, 4.01};
String [] m = {"January", "February", ...., "November", "December"};

// create the 12 CalcStock object
CalcStock[] cs = new CalcStock[12];
for(int i = 0; i < 12; i++)
cs[i] = new CalcStock(p[i], m[i]);

// now you can do your loops
CalcStock max = cs[0];

for(int i = 1; i < 12; i++) {
max = max.getMax(cs[i]);
}
// ok now max.p = is the largest value and max.m is the month
}
}

```

This post has been edited by pbl: 07 January 2010 - 06:16 PM

### #6 larrybalz4life

Reputation: 0
• Posts: 7
• Joined: 06-January 10

## Re: Referencing Two set of arrays with different data type String and Numb

Posted 07 January 2010 - 06:19 PM

Works well. I have edited the code to get Highest and Lowest Month.
Out of curiosity.. Is there are way of getting the same result by comparing array with different datatypes (Sting and Double). Because i would like to know if that is possible.
Please all response would be appreciated.

```class CalcStock {
private double p;
private String m;

// constructor
CalcStock(double price, String month) {
p = price;
m = month;
}

public static void main(String[] args) {
double [] p = {4.5, 4.63, 3.65,4.70,4.72,4.21,5.02,4.81,4.10,4.11,4.09,4.01};
String [] m = {"January","February","March","April","May", "June","July","August","September","October","November", "December"};

// create the 12 CalcStock object
CalcStock[] cs = new CalcStock[12];
for(int i = 0; i < 12; i++)
cs[i] = new CalcStock(p[i], m[i]);

// now you can do your loops
double max = cs[0].p;
int maxIndex = 0;
int minIndex =0;
for(int i = 1; i < 12; i++) {
if(cs[i].p > max) {
max = cs[i].p;
maxIndex = i;
}
if(cs[i].p< max) {
double min = cs[i].p;
minIndex = i;
}
}
// ok now cs[maxIndex] is the biggest month and its name is cs[maxIndex].m
// ok now cs[minIndex] is the lowest month and its name is cs[minIndex].m
System.out.println("The Best month value is= " + cs[maxIndex].m);
System.out.println("The Worst month value is= " + cs[minIndex].m);
}
}

```

This post has been edited by larrybalz4life: 07 January 2010 - 06:20 PM

### #7 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Referencing Two set of arrays with different data type String and Numb

Posted 07 January 2010 - 06:30 PM

larrybalz4life, on 7 Jan, 2010 - 05:19 PM, said:

Works well. I have edited the code to get Highest and Lowest Month.
Out of curiosity.. Is there are way of getting the same result by comparing array with different datatypes (Sting and Double). Because i would like to know if that is possible.
Please all response would be appreciated.

No way.... you cannot compare double with String
but finding the highest of double you can use it's index for the second one
and... I have edited my previous post for an even cleaner way

### #8 larrybalz4life

Reputation: 0
• Posts: 7
• Joined: 06-January 10

## Re: Referencing Two set of arrays with different data type String and Numb

Posted 07 January 2010 - 06:57 PM

pbl, on 7 Jan, 2010 - 05:30 PM, said:

larrybalz4life, on 7 Jan, 2010 - 05:19 PM, said:

Works well. I have edited the code to get Highest and Lowest Month.
Out of curiosity.. Is there are way of getting the same result by comparing array with different datatypes (Sting and Double). Because i would like to know if that is possible.
Please all response would be appreciated.

No way.... you cannot compare double with String
but finding the highest of double you can use it's index for the second one
and... I have edited my previous post for an even cleaner way

Ok, looks good but i have not tried it yet.. tho..
So can you compare using the index without creating objects as seen in you code above..
I mean, just a little tweak of my first post.. Because i was playing around with it and i got results of months/prices but couldn't get the same result as the one you helped me with..

java script:emoticon('',%20'smid_8')

### #9 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12278
• Posts: 45,364
• Joined: 27-December 08

## Re: Referencing Two set of arrays with different data type String and Numb

Posted 07 January 2010 - 08:08 PM

You can compare indices to each other, which are type int. However, you can never compare String and double unless you want your compiler to barf all over you (a fancy way of saying fail to compile your program ).

Also, you should start to think about using classes instead of parallel arrays in your program design as Pbl has already pointed out. I've written a tutorial on the subject. If you hit the My Contributions link under my post, you should immediately see it. Working with classes will make your life a whole lot easier as you develop (no pun intended) as a programmer.

This post has been edited by macosxnerd101: 07 January 2010 - 08:19 PM

### #10 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Referencing Two set of arrays with different data type String and Numb

Posted 07 January 2010 - 08:10 PM

macosxnerd101, on 7 Jan, 2010 - 07:08 PM, said:

Also, you should start to think about using classes instead of parallel arrays in your program design.

Macos have you read my post ?

### #11 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12278
• Posts: 45,364
• Joined: 27-December 08

## Re: Referencing Two set of arrays with different data type String and Numb

Posted 07 January 2010 - 08:18 PM

pbl, on 7 Jan, 2010 - 11:10 PM, said:

macosxnerd101, on 7 Jan, 2010 - 07:08 PM, said:

Also, you should start to think about using classes instead of parallel arrays in your program design.

Macos have you read my post ?

Sorry, I have. I forgot to include the "as Pbl has already pointed out." I'll edit the above post.

### #12 g00se

• D.I.C Lover

Reputation: 3536
• Posts: 16,028
• Joined: 20-September 08

## Re: Referencing Two set of arrays with different data type String and Numb

Posted 08 January 2010 - 01:17 AM

Quote

Sorry, I have. I forgot to include the "as Pbl has already pointed out." I'll edit the above post.

### #13 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Referencing Two set of arrays with different data type String and Numb

Posted 08 January 2010 - 06:11 PM

g00se, on 8 Jan, 2010 - 12:17 AM, said:

Quote

Sorry, I have. I forgot to include the "as Pbl has already pointed out." I'll edit the above post.

The principle is good but... I am the one who provided the example

### #14 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12278
• Posts: 45,364
• Joined: 27-December 08

## Re: Referencing Two set of arrays with different data type String and Numb

Posted 08 January 2010 - 07:01 PM

pbl, on 8 Jan, 2010 - 09:11 PM, said:

The principle is good but... I am the one who provided the example

I'm just trying to off the OP a little more reinforcement on using classes by directing him to my tutorial, not upstage anyone.