Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. package lessons;
  2. import java.util.ArrayList;
  3.  
  4. public class combinations {
  5. // the initial array
  6. ArrayList<Integer> arr1=new ArrayList<Integer>();
  7. static ArrayList<String> allwords=new ArrayList<String>();
  8. //at the begining string is zero str is the string that would be added to the array
  9. String str="";
  10. String newstr;
  11. //temp string
  12. String temp="";
  13. //initial string from the array
  14. String instr="";
  15. String a="";
  16. String b="";
  17. String c="";
  18. int k=1;
  19. // the array that the devide would store
  20. //ArrayList<String> al=new ArrayList<String>();
  21. // the method to find the string from the array
  22. public String inString(ArrayList arr3){
  23. for(int i=0;i<arr3.size(); i++){
  24. a=arr3.get(i).toString();
  25. instr=instr+a;
  26. }
  27. return instr;
  28. }
  29. public String replace(String oldstr, String ch){
  30. //int index=oldstr.indexOf(ch);
  31. newstr=oldstr.replaceFirst(ch, "");
  32. return newstr;
  33. }
  34. public boolean recurse(ArrayList<String> arr2, int size){
  35. if(arr2.size()==1){
  36. str=str+arr2.get(0);
  37. if(allwords.size()==0){
  38. allwords.add(str);
  39. }else{
  40. for(int i=0; i<allwords.size();i++){
  41. if(allwords.get(i).equals(str)){
  42. break;
  43. }
  44. if(i==allwords.size()-1){
  45. allwords.add(str);
  46. }
  47. }}
  48. //allwords.add(str);
  49. str=str.replaceFirst(arr2.get(0), "");
  50. return true;
  51. }
  52. for(int i=0; i<arr2.size(); i++){
  53. if(i!=0){
  54. a=arr2.get(0);
  55. b=arr2.get(i);
  56. arr2.set(0, b);
  57. arr2.set(i, a);
  58. }
  59. if(k==arr2.size()){
  60. k=0;
  61. }
  62. ArrayList<String> al=new ArrayList<String>(arr2.subList(k, arr2.size()));
  63. str=str+arr2.get(0);
  64. recurse(al,size);
  65. str=str.replaceFirst(arr2.get(0), "");
  66. }
  67. return true;
  68. }
  69. public static void main(String[] args){
  70. combinations comb=new combinations();
  71. ArrayList<String> aaa=new ArrayList<String>();
  72. aaa.add("1");
  73. aaa.add("2");
  74. aaa.add("3");
  75. aaa.add("4");
  76. aaa.add("5");
  77. aaa.add("5");
  78. comb.inString(aaa);
  79. comb.recurse(aaa,0);
  80. for(int i=0; i<allwords.size();i++){
  81. int t=i+1;
  82. System.out.println(allwords.get(i)+" "+t);
  83. }
  84. /* combinations com=new combinations();
  85. String s="Hello world";
  86. String s1="l";
  87. System.out.println(com.replace(s, s1));*/
  88.  
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement