Chat LIVE With Programming Experts! There Are 23 Online Right Now...

 

Code Snippets

  

Java Source Code


Welcome to Dream.In.Code
Become a Java Expert!

Join 244,295 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 895 people online right now. Registration is fast and FREE... Join Now!





Array operations

Want to resize an array, apppend an item, insert an item, delete some offsets? This class will perform all that for you!

Submitted By: alpha02
Actions:
Rating:
Views: 3,547

Language: Java

Last Modified: July 19, 2007
Instructions: Call the self-explanatory static methods to perform array operations.

Snippet


  1.  
  2. /*This class is used to make operations with arrays.*/
  3.  
  4. public class Array {
  5.      public static String[] remove(String[] a, int b){ //Remove a specific offset
  6.           if (b >= a.length || b < 0){return a;}
  7.           String[] rtn = new String[a.length-1];
  8.           int j=0;
  9.           for (int i=0; i<a.length; i++){
  10.                if (i != b){
  11.                     rtn[j] = a[i];
  12.                     j += 1;
  13.                }
  14.           }
  15.           return rtn;
  16.      }
  17.      
  18.      public static String[][] remove(String[][] a, int b){ //Remove a specific offset
  19.           if (b >= a.length || b < 0){return a;}
  20.           String[][] rtn = new String[a.length-1][a[0].length];
  21.           int j=0;
  22.           for (int i=0; i<a.length; i++){
  23.                if (i != b){
  24.                     rtn[j] = a[i];
  25.                     j += 1;
  26.                }
  27.           }
  28.           return rtn;
  29.      }
  30.      
  31.      public static String[] remove(String[] a, int[] b){ //Remove specific offsets
  32.           int j = 0;
  33.           boolean l = false;
  34.           for (int i=0; i<b.length; i++){
  35.                l = true;
  36.                if (b[i] >= 0 && b[i] < a.length){
  37.                     for (int m=0; m<b.length; m++){
  38.                          if (m != i && b[m] == b[i]){
  39.                               l = false;
  40.                               m = b.length;
  41.                          }
  42.                     }
  43.                     if (l == true){j += 1;}
  44.                }
  45.           }
  46.           int[] c = new int[j];
  47.           j=0;
  48.           for (int i=0; i<b.length; i++){
  49.                l = true;
  50.                if (b[i] >= 0 && b[i] < a.length){
  51.                     for (int m=0; m<b.length; m++){
  52.                          if (m != i && b[m] == b[i]){
  53.                               l = false;
  54.                               m = b.length;
  55.                          }
  56.                     }
  57.                     if (l == true){
  58.                          c[j] = b[i];
  59.                          j += 1;
  60.                     }
  61.                }
  62.           }
  63.           String[] rtn = new String[a.length-j];
  64.           j=0;
  65.           for (int i=0; i<a.length; i++){
  66.                l = true;
  67.                for (int k=0; k<c.length; k++){
  68.                     if (c[k] == i){
  69.                          l = false;
  70.                          k = c.length;
  71.                     }
  72.                }
  73.                if (l == true){
  74.                     rtn[j] = a[i];
  75.                     j += 1;
  76.                }
  77.           }
  78.           return rtn;
  79.      }
  80.      
  81.      public static String[][] remove(String[][] a, int[] b){ //Remove specific offsets
  82.           int j = 0;
  83.           boolean l = false;
  84.           for (int i=0; i<b.length; i++){
  85.                l = true;
  86.                if (b[i] >= 0 && b[i] < a.length){
  87.                     for (int m=0; m<b.length; m++){
  88.                          if (m != i && b[m] == b[i]){
  89.                               l = false;
  90.                               m = b.length;
  91.                          }
  92.                     }
  93.                     if (l == true){j += 1;}
  94.                }
  95.           }
  96.           int[] c = new int[j];
  97.           j=0;
  98.           for (int i=0; i<b.length; i++){
  99.                l = true;
  100.                if (b[i] >= 0 && b[i] < a.length){
  101.                     for (int m=0; m<b.length; m++){
  102.                          if (m != i && b[m] == b[i]){
  103.                               l = false;
  104.                               m = b.length;
  105.                          }
  106.                     }
  107.                     if (l == true){
  108.                          c[j] = b[i];
  109.                          j += 1;
  110.                     }
  111.                }
  112.           }
  113.           String[][] rtn = new String[a.length-j][a[0].length];
  114.           j=0;
  115.           for (int i=0; i<a.length; i++){
  116.                l = true;
  117.                for (int k=0; k<c.length; k++){
  118.                     if (c[k] == i){
  119.                          l = false;
  120.                          k = c.length;
  121.                     }
  122.                }
  123.                if (l == true){
  124.                     rtn[j] = a[i];
  125.                     j += 1;
  126.                }
  127.           }
  128.           return rtn;
  129.      }
  130.      
  131.      public static String[] append (String[] a, String b){ //Append a string
  132.           String[] rtn = new String[a.length+1];
  133.           for (int i=0; i<a.length; i++){rtn[i] = a[i];}
  134.           rtn[a.length] = b;
  135.           return rtn;
  136.      }
  137.      
  138.      public static String[][] append (String[][] a, String[] b){ //Append a string
  139.           if (b.length == 0){return a;}
  140.           String[][] rtn = new String[a.length+1][b.length];
  141.           for (int i=0; i<a.length; i++){rtn[i] = a[i];}
  142.           rtn[a.length] = b;
  143.           return rtn;
  144.      }
  145.      
  146.      public static String[] append(String[] a, String[] b){ //Append strings
  147.           if (b.length == 0){return a;}
  148.           String[] rtn = new String[a.length+b.length];
  149.           for (int i=0; i<a.length; i++){rtn[i] = a[i];}
  150.           for (int i=a.length; i<a.length+b.length; i++){rtn[i] = b[i-a.length];}
  151.           return rtn;
  152.      }
  153.      
  154.      public static String[][] append(String[][] a, String[][] b){ //Append strings
  155.           if (b.length == 0){return a;}
  156.           String[][] rtn = new String[a.length+b.length][b.length];
  157.           for (int i=0; i<a.length; i++){rtn[i] = a[i];}
  158.           for (int i=a.length; i<a.length+b.length; i++){rtn[i] = b[i-a.length];}
  159.           return rtn;
  160.      }
  161.      
  162.      public static String[] insert(String[] a, String b, int c){ //Insert a string
  163.           if (c < 0 || c > a.length){return a;}
  164.           String[] rtn = new String[a.length+1];
  165.           int j=0;
  166.           for (int i=0; i<a.length; i++){
  167.                if (j == c){
  168.                     rtn[j] = b;
  169.                     j += 1;
  170.                }
  171.                rtn[j] = a[i];
  172.                j += 1;
  173.           }
  174.           return rtn;
  175.      }
  176.      
  177.      public static String[][] insert(String[][] a, String[] b, int c){ //Insert a string
  178.           if (c < 0 || c > a.length || b.length == 0){return a;}
  179.           String[][] rtn = new String[a.length+1][b.length];
  180.           int j=0;
  181.           for (int i=0; i<a.length; i++){
  182.                if (j == c){
  183.                     rtn[j] = b;
  184.                     j += 1;
  185.                }
  186.                rtn[j] = a[i];
  187.                j += 1;
  188.           }
  189.           return rtn;
  190.      }
  191.      
  192.      public static String[] insert(String[] a, String[] b, int c){ //Insert strings
  193.           if (c < 0 || c > a.length || b.length == 0){return a;}
  194.           String[] rtn = new String[a.length+b.length];
  195.           int j=0;
  196.           for (int i=0; i<a.length; i++){
  197.                if (j == c){
  198.                     for (int k=0; k<b.length; k++){
  199.                          rtn[j+k] = b[k];
  200.                     }
  201.                     j += b.length;
  202.                }
  203.                rtn[j] = a[i];
  204.                j += 1;
  205.           }
  206.           return rtn;
  207.      }
  208.      
  209.      public static String[][] insert(String[][] a, String[][] b, int c){ //Insert strings
  210.           if (c < 0 || c > a.length || b.length == 0){return a;}
  211.           String[][] rtn = new String[a.length+b.length][b.length];
  212.           int j=0;
  213.           for (int i=0; i<a.length; i++){
  214.                if (j == c){
  215.                     for (int k=0; k<b.length; k++){
  216.                          rtn[j+k] = b[k];
  217.                     }
  218.                     j += b.length;
  219.                }
  220.                rtn[j] = a[i];
  221.                j += 1;
  222.           }
  223.           return rtn;
  224.      }
  225.      
  226.      public String[] fill(String[] a, String b){ //Fill an array with something
  227.           String[] rtn = a;
  228.           for (int i=0; i<a.length; i++){rtn[i] = b;}
  229.           return rtn;
  230.      }
  231. }
  232.  

Copy & Paste


Comments


nhel 2008-08-05 02:32:58

can you teach me how to create a simple program which will ask the user to input ten names ans then another input dialog box which will ask the user to type the name to be searched in the input by the user. thanks! more power. you can email me at andradaneil@yahoo.com

aforlion 2009-02-20 08:49:59

Pls, can you teach me how to create a simple program which will ask the user to input ten names ans then another input dialog box which will ask the user to type the name to be searched in the input by the user.you can at aforlion@yahoo.co.uk. Thanks, you're doing great

ivey.eli 2009-06-20 00:18:39

Nice work


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month