15,614,809 members
See more:
String[] stockPrice = { "Reliance", "2022-01-01 10:00", "100",
"Reliance", "2022-01-01 10:34", "110",
"Tata", "2022-01-01 10:30", "500",
"Reliance", "2022-03-02 10:15", "120",
"Tata", "2022-01-01 12:00", "400" };

This is given array in which I have to print the company name , date and average price.

So in array company having prices on same date but different time , so I have to calculate the average of stock price.

Output:

ComapnyName Date Avg
------------------------------------------
Relaince 2022-01-01 105
Relaince 2022-03-02 120
Tata 2022-01-01 450

I tried but how will I check that company having different prices at same date .
what should I try ?
Its a single dimensional array.

What I have tried:

public class CompanyStocks {

public static void main(String[] args) {

// ArrayList<string> compNameList = new ArrayList<>();
// ArrayList<string> compTimeStamps = new ArrayList<>();
// ArrayList<string> compStockPrize = new ArrayList<>();
//
String[] stockPrice = { "Reliance", "2022-01-01 10:00", "100",
"Reliance", "2022-01-01 10:34", "110",
"Tata", "2022-01-01 10:30", "500",
"Reliance", "2022-03-02 10:15", "120",
"Tata", "2022-01-01 12:00", "400" };
for(int i=0; i
Posted
Updated 17-Jun-22 4:08am
v6

## Solution 1

You should make it into a multi-dimensional array like:
```String[][] stockPrice = {
{ "Reliance", "2022-01-01 10:00", "100" },
{ "Reliance", "2022-01-01 10:34", "110" },
{ "Tata", "2022-01-01 10:30", "500" },
{ "Reliance", "2022-03-02 10:15", "120" },
{ "Tata", "2022-01-01 12:00", "400" },
};```

You can now process each individual set and calculate the averages by creating a new Map collection for each product name. See Lesson: Interfaces (The Java™ Tutorials > Collections)[^].

Member 13102977 17-Jun-22 10:08am
This question is asked in an interview , and its a single dimensional array , I able to print the company , timestamps and price of different companies but problem is that if same company have different price on same date but different time , so I have calculate the average price of same company , so how we do this ?
Richard MacCutchan 17-Jun-22 10:42am
The way I suggested in my answer. Create new lists for each company, and add all items belonging to each compnay to its own list. You can then calculate averages etc. Alternatively use each company name to calculate the total of all its prices and the number of entries. Again use that data to calculate the average.

Top Experts
Last 24hrsThis month
 OriginalGriff 110 raddevus 50 Richard MacCutchan 40 CHill60 35 Graeme_Grant 25
 OriginalGriff 2,966 Richard MacCutchan 1,266 Graeme_Grant 965 Dave Kreskowiak 649 CHill60 360

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900