简体   繁体   中英

java 2D array search/sort/add

I have to take in a few ID numbers and how many boxes sold by the ID number...split those into two arrays...which I've done....then sort the array reporting the largest sale of boxes sold....the(and this is my problem) if the same id number is entered, add the number of boxes sold together and make it take up only one spot in the array

example....

idNum 1 = 100
idNum 3 = 500
idNum 1 = 200


so idNum 1 = 300




               //declarations
               int [][] stupidKids = new int[10][2];//10 rows....2 columns
               int entry = 0;
               int tempNum;
               int tempTotal = 0;
               int tempId;
               int found = -1;
               //user input section
               for(int i = 0; i < 2; i++)
               {
               System.out.print("Please enter the class ID");
               Scanner scan = new Scanner(System.in);
               stupidKids[i][0] = scan.nextInt();

               System.out.print("Please enter the amount of cookies sold");
               stupidKids[i][1] = scan.nextInt();

               System.out.println(stupidKids[i][0] + "Cookies " + stupidKids[i][1]);
                }

               //sorting process
               for(int i = 0; i < 2; i++)
               {
                     if(stupidKids[i][1] > stupidKids[i+1][1])
                     {
                        tempNum = stupidKids[i][1];
                        stupidKids[i][1] = stupidKids[i + 1][1];
                        stupidKids[i + 1][1] = tempNum;

                        tempId = stupidKids[i][0];
                        stupidKids[i][0] = stupidKids[i+1][0];
                        stupidKids[i+1][0] = tempId;
                     }


               }


               //comparing for same ID Num
               for(int i = 0; i < 1; i++)
                  {
                     if(stupidKids[i][0] == stupidKids[i+1][0])
                       {
                          tempTotal = (stupidKids[i][1] + stupidKids[i+1][1]); 
                       }
                  }
                  System.out.println("Final num " + tempTotal);

You must loop through your for loops 10 times rather than just twice, this way you get through the whole array.

     for(int i = 0; i < 10; i++)

Below is the code to compare the IDs against each other in the 2D array. Although I am not quite sure about why you would want it to take up only one space per ID as you will end up with a lot of NULL values in the array as you can not easily resize an array (to do this requires an implementation of a basic algorithm)

              for(int i = 0; i < 10; i++)
              {
                 tempID = stupidKids[i][0]; 

                 for(int j = 0; j < 10; j++) 
                 {
                    if( i != j)
                    {
                       if(stupidKids[i][0] == stupidKids[j][0])
                       {
                          tempTotal = (stupidKids[i][1] + stupidKids[j][1]); 
                       } 
                    }
                 }  
              }
              System.out.println("Final num " + tempTotal);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM