program-o-steve
program-o-steve

Reputation: 2678

calculating the final length

The following code separates the duplicate names into 1 column and sum of numbers associated with the names into the second column.

Like :

   Nokia 21

   Blackberry 3

   Nimbus 30

from the array given in the program.


I want to know the final length of the array that contain these entries. In this case 3. How do i calculate that ?

package keylogger;
import java.util.ArrayList;
import java.util.List;

public class ArrayTester {

private static int finalLength = 0;
private static String Name[][];
private static String data[][] = { 
                               {"Nokia" , "7"},
                               {"Blackberry" ,"1"},
                               {"Nimbus","10"},
                               {"Nokia" , "7"},
                               {"Blackberry" , "1"},
                               {"Nimbus","10"},
                               {"Nokia" , "7"},
                               {"Blackberry" , "1"},
                               {"Nimbus","10"}

                          };  

public void calculator() {

    Name = new String[data.length][2];
    List<String> marked = new ArrayList<String>();
    try {
        for(int i=0;i<data.length;i++) {
            Name[i][0] = data[i][0];
            Name[i][1] = data[i][1];
            String name = data[i][0];
            if(marked.contains(name)) {

                continue;
            }
            marked.add(name);
            int k = i + 1;
            int v = k;
            for (int j = 0; j < data.length - v; j++) {
                String s = data[k][0];
                if(Name[i][0].equalsIgnoreCase(s)) {
                    Name[i][0] = s;
                    Integer z = Integer.parseInt(Name[i][1]) + Integer.parseInt(data[k][1]);
                    Name[i][1] = z.toString();
                }
                k++;
            }

        }
    }catch(Exception exc) {
        exc.printStackTrace();
    }
}

public static void main(String args[]) {
    ArrayTester o = new ArrayTester();
    o.calculator();
    for(String s[] : Name) {
        for(String x : s) {
            System.out.println(x);
        }
    }
}

}

Upvotes: 0

Views: 152

Answers (3)

brimborium
brimborium

Reputation: 9512

As far as I understand it, you want to know the number of distinct names in your array without calling calculator(), right? I don't really know if that makes sense as you still have to go through every entry and compare it with a set. But you could do it with a Set:

private int getNumberOfEntries(String[][] data) {
  Set<String> names = new HashSet<String>();
  for (int i=0; i<data.length; i++) {
    names.add(data[i][1]);
  }
  return names.size();
}

Now you can just call int n = getNumberOfEntries(data);...

EDIT: Of course it makes more sense to do the sums in the same step, see Bohemians solution for that.

Upvotes: 2

Bohemian
Bohemian

Reputation: 424973

As usual, the "problem" is poor coding. Your entire program, properly written, can be reduced to just 3 lines of code (5 if you include defining the array and printing the output):

public static void main(String[] args) {
    String data[][] = {{"Nokia", "7"}, {"Blackberry", "1"}, {"Nimbus", "10"},
        {"Nokia", "7"}, {"Blackberry", "1"}, {"Nimbus", "10"}, {"Nokia", "7"},
        {"Blackberry", "1"}, {"Nimbus", "10"}, {"Zebra", "78"}};

    HashMap<String, Integer> totals = new HashMap<String, Integer>();
    for (String[] datum : data)
        totals.put(datum[0], new Integer(datum[1]) + (totals.containsKey(datum[0]) ? totals.get(datum[0]) : 0));
    System.out.println("There are " + totals.size() + " brands: " + totals);
}

Output:

There are 4 brands: {Nimbus=30, Zebra=78, Nokia=21, Blackberry=3}

Upvotes: 2

Jack
Jack

Reputation: 133557

You can't know it a priori, the size will be known just when you'll have finished splitting the strings and doing your math.

In your example in the end marked.size() will have the size you are looking for but I'd suggest you to directly use a HashMap so that you won't care about searching for existing elements in linear time and then convert it to an array.

Something like:

String[][] names = new String[map.size()];
Set<String> keys = map.keys();
int c = 0;

for (String k : keys)
{
  names[c] = new String[2];
  names[c][0] = k;
  names[c++][1] = map.get(k).toString();
}

Upvotes: 2

Related Questions