hello everyone. am omar. i like computers. am new to this forum and i've got sooooo many questions to ask you. java is a prg language that i dont like much but well, i have to learn about it. future and all that shit. anywyz, i'll b posting a lot of questions and enquiries this week. man, have i got a shit load to learn about here's my first question i've got two integer sorted arrays in ascending order. am supposed to merge these into a third array WITHOUT using a sorting algorithm. so this means that i cant just throw them in a third array and then sort it. nope, that wont work. plus i dont want to have duplicates. if first is 1,2,3 and second is 0,1,10 then third must b 0,1,2,3,10. i've tried so many ways but i just cant get it right. plz keep it simple ppl. am still new this prg language. :) thank you
You can use a Hashtable object obtain your goal. Try this snippet: public int[] mergeArrays(int[] firstarray, int[] secondarray){ Hashtable hash = new Hashtable(); for (int i=0; i<firstarray.length; i++) hash.put(new Integer(firstarray[i]), new Integer(firstarray[i])); for (int i=0; i<secondarray.length; i++) hash.put(new Integer(secondarray[i]), new Integer(secondarray[i])); Enumeration enum = hash.keys(); int intArrayResult = new int[firstarray.length + secondarray.length]; int index=0; while (enum.hasNext()){ intArrayResult[index] = (Integer)enum.nextElement(); index++; } return intArrayResult }
thanx for the reply but you see, the book that am learning this in (the chp am at now) doesnt have anything about hash. isnt there any other way? but thank you for ur reply.
Hash and Enumeration are just the means we use to get your goal. If u don't want to study new things, its ok, but how can you handle your problems in general with a huge lack of tools.
by the way, if u want to use the code i gave, there is a small wrong at the while loop, change this way: