Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JOptionPane;
- public class ArrayManip
- {
- public static void main(String [] args)
- {
- testSearchNum();
- testHasDuplicates();
- testRemoveDuplicates();
- testContainsAll();
- testCommonElements();
- }
- public static boolean searchNum(int [] myArray, int num)
- {
- boolean containsNum = false;
- for(int i = 0 ; i < myArray.length ; i++)
- {
- if(myArray[i] == num)
- containsNum = true;
- }
- return containsNum;
- }
- public static void testSearchNum()
- {
- String result = "";
- int x;
- String msg1 = "Please specify the size of the array";
- x = Integer.parseInt(JOptionPane.showInputDialog(null, msg1));
- int[] myArray = new int[x];
- String msg2 = "Please enter " + x + " integers(allow a space between each integer)";
- String input = JOptionPane.showInputDialog(null, msg2);
- String[] temp = new String[x];
- temp = input.split(" ");
- for(int i = 0 ; i < x ; i++)
- myArray[i] = Integer.parseInt(temp[i]);
- String msg3 = "Please enter the number you would like to search for";
- int num = Integer.parseInt(JOptionPane.showInputDialog(null, msg3));
- boolean contains = searchNum(myArray,num);
- if(contains)
- result += "Array contains number";
- else
- result += "Array does not contain number";
- JOptionPane.showMessageDialog(null, result);
- }
- public static boolean hasDuplicates(int[] listA)
- {
- boolean duplicates = false;
- for(int i = 0 ; i < listA.length ; i++)
- {
- for(int j = i+1 ; j < listA.length ; j++)
- {
- if(listA[i] == listA[j])
- duplicates = true;
- }
- }
- return duplicates;
- }
- public static void testHasDuplicates()
- {
- String result = "";
- int x;
- String msg1 = "Please specify the size of the array";
- x = Integer.parseInt(JOptionPane.showInputDialog(null, msg1));
- int[] listA = new int[x];
- String msg2 = "Please enter " + x + " integers(allow a space between each integer)";
- String input = JOptionPane.showInputDialog(null, msg2);
- String[] temp = new String[x];
- temp = input.split(" ");
- for(int i = 0 ; i < x ; i++)
- listA[i] = Integer.parseInt(temp[i]);
- boolean duplicates = hasDuplicates(listA);
- if(duplicates)
- result += "Duplicates found";
- else
- result += "No duplicates found";
- JOptionPane.showMessageDialog(null, result);
- }
- public static int[] removeDuplicates(int[] listA)
- {
- int k = listA.length;
- for(int i = 0 ; i < k - 1 ; i++)
- {
- for(int j = i+1 ; j < k ; )
- {
- if(listA[i] == listA[j] && j < k)
- {
- listA[j] = listA[k-1];
- k--;
- }
- else
- j++;
- }
- }
- int[] noDuplicates = new int[k];
- for(int x = 0 ; x < k ; x++)
- noDuplicates[x] = listA[x];
- return noDuplicates;
- }
- public static void testRemoveDuplicates()
- {
- String result = "";
- int x;
- String msg1 = "Please specify the size of the array";
- x = Integer.parseInt(JOptionPane.showInputDialog(null, msg1));
- int[] listA = new int[x];
- String msg2 = "Please enter " + x + " integers(allow a space between each integer)";
- String input = JOptionPane.showInputDialog(null, msg2);
- String[] temp = new String[x];
- temp = input.split(" ");
- for(int i = 0 ; i < x ; i++)
- listA[i] = Integer.parseInt(temp[i]);
- int[] newArray = removeDuplicates(listA);
- String[] myArray = new String[newArray.length];
- for(int j = 0 ; j < newArray.length ; j++)
- myArray[j] = Integer.toString(newArray[j]);
- for(int y = 0 ; y < myArray.length ; y++)
- result += myArray[y] + " ";
- JOptionPane.showMessageDialog(null, result);
- }
- public static boolean containsAll(int [] listA, int[] listB)
- {
- boolean contains = false;
- for(int i = 0; i < listA.length ; i++)
- {
- contains = false;
- for(int j = 0; j < listB.length && !contains ; j++)
- if(listA[i] == listB[j])
- contains = true;
- }
- return contains;
- }
- public static void testContainsAll()
- {
- String result = "";
- int x, y;
- String msg1 = "Please specify the size of listA";
- x = Integer.parseInt(JOptionPane.showInputDialog(null, msg1));
- String msg2 = "Please specify the size of listB";
- y = Integer.parseInt(JOptionPane.showInputDialog(null, msg2));
- String msg3 = "Please enter " + x + " integers(allow a space between each integer)";
- String input1 = JOptionPane.showInputDialog(null, msg3);
- String msg4 = "Please enter " + y + " integers(allow a space between each integer)";
- String input2 = JOptionPane.showInputDialog(null, msg4);
- String[] temp1 = new String[x];
- temp1 = input1.split(" ");
- String[] temp2 = new String[y];
- temp2 = input2.split(" ");
- int[] listA = new int[x];
- int[] listB = new int[y];
- for(int i = 0 ; i < x ; i++)
- listA[i] = Integer.parseInt(temp1[i]);
- for(int j = 0 ; j < y ; j++)
- listB[j] = Integer.parseInt(temp2[j]);
- boolean subset = containsAll(listA, listB);
- if(subset)
- result += "All the elements in listA are in listB";
- else
- result += "All the elements in listA are not in listB";
- JOptionPane.showMessageDialog(null, result);
- }
- public static int[] commonElements(int[] listA, int[] listB)
- {
- int[] temp = new int[listA.length];
- int count = 0;
- boolean found = false;
- for(int i = 0; i < listA.length; i++)
- {
- found = false;
- for(int j = 0 ; j < listB.length && !found ; j ++)
- {
- if(listA[i]==listB[j])
- {
- found = true;
- temp[count] = listA[i];
- count++;
- }
- }
- }
- int[] commonList = new int[count];
- for(int k = 0 ; k < count ; k++)
- commonList[k] = temp[k];
- return commonList;
- }
- public static void testCommonElements()
- {
- String result = "Common Elements from listA and listB are:\n";
- int x, y;
- String msg1 = "Please specify the size of listA";
- x = Integer.parseInt(JOptionPane.showInputDialog(null, msg1));
- String msg2 = "Please specify the size of listB";
- y = Integer.parseInt(JOptionPane.showInputDialog(null, msg2));
- String msg3 = "Please enter " + x + " integers(allow a space between each integer)";
- String input1 = JOptionPane.showInputDialog(null, msg3);
- String msg4 = "Please enter " + y + " integers(allow a space between each integer)";
- String input2 = JOptionPane.showInputDialog(null, msg4);
- String[] temp1 = new String[x];
- temp1 = input1.split(" ");
- String[] temp2 = new String[y];
- temp2 = input2.split(" ");
- int[] listA = new int[x];
- int[] listB = new int[y];
- for(int i = 0 ; i < x ; i++)
- listA[i] = Integer.parseInt(temp1[i]);
- for(int j = 0 ; j < y ; j++)
- listB[j] = Integer.parseInt(temp2[j]);
- int[] common = commonElements(listA, listB);
- int[] common2 = removeDuplicates(common);
- for(int k = 0 ; k < common2.length; k ++)
- result += common2[k] + "\t";
- JOptionPane.showMessageDialog(null, result);
- }
- public static int[] joinArrays(int[] listA, int[] listB)
- {
- int length = listA.length + listB.length;
- int[] joined = new int[length]
- int j=0;
- for(int i = 0; i < listA.length ; i++)
- {
- joined[i] = listA[i];
- j = j+1;
- }
- for(int k = j ; k < listB.length ; k++)
- joined[k] = listB[k];
- return joined;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement