I'd like some assistance on comparing two arrays of different dimensions. I want to check if the single dimensional array is a sub array of the 2 dimensional array. Here's what I tried:
public static void compare() {
int[][] x = {{23, 33, 46, 50, 56},
{3, 8, 65, 34, 90},
{2, 7, 46, 50, 56}};
int[] y = {2, 7, 46, 50, 56};
for (int i = 0; i < x.length - (x.length) + 1; i++) {
System.out.println(Arrays.toString(x[2]));
for (int j = 0; j < y.length - (y.length) + 1; j++) {
System.out.println(Arrays.toString(y));
if (x[2].equals(y)) {
System.out.println("match");
} else {
System.out.println("no match");
}
}
}
}
I don't know what exactly you want, but as i can see - you need something like:
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[][] x = {{23, 33, 46, 50, 56},
{3, 8, 65, 34, 90},
{2, 7, 46, 50, 56}};
int[] y = {2, 7, 46, 50, 56};
for (int[] aX : x) {
System.out.println(Arrays.toString(x[2]));
if (Arrays.equals(aX, y)) {
System.out.println("match");
} else {
System.out.println("no match");
}
}
}
}
int[][] x = {{23, 33, 46, 50, 56},
{3, 8, 65, 34, 90},
{2, 7, 46, 50, 56}};
int[] y = {2, 7, 46, 50, 56};
for (int[] i : x) {
if (Arrays.equals(y, i)) {
System.out.println("match");
}
}
import java.util.*;
public class ArrayMatch {
public static void main(String[] args){
int[][] x = {{23, 33, 46, 50, 56},
{3, 8, 65, 34, 90},
{2, 7, 46, 50, 56}};
int[] y = {2, 7, 46, 50, 56};
String yArray = Arrays.toString(y);
boolean match = false;
for(int i = 0; i < x.length; i++) {
String comparison = Arrays.toString(x[i]);
if(comparison.equals(yArray)) {
match = true;
}
}
if(match) {
System.out.println("Match");
}
else
System.out.println("No match");
}
}
Here is your output:
Match
int[][] x = { { 23, 33, 46, 50, 56 }, { 3, 8, 65, 34, 90 }, { 2, 7, 46, 50, 56 } };
int[] y = { 2, 7, 46, 50, 56 };
int[] z = x[2].clone();
int count = 0;
for (int i = 0; i < z.length; i++) {
if (z[i] != y[i]) {
System.out.println("Not Match");
break;
} else {
count++;
if (count == y.length) {
System.out.println("Match");
break;
} else {
continue;
}
}
}
Output is:
Match
I have copied 2D Array's 2nd element to another array(z[]) because array can be compared to one by one element not directly one array to another array.
Related
So I've been working on this code for quite a while, and I feel I am almost done. However, I keep running into the Stack overflow error over and over and I can't seem to fix it. I want to be able to print out the recursive code after the standard code, but I seem to get the error sometime in the mergesort method. I can't figure out what is causing the error even after looking up recursion and stack overflow. I need help. This is a recursive merge method.
import java.util.*;
import java.lang.*;
import java.io.*;
public class merge_recursive {
public void mergeSort(ArrayList <Comparable> a, int first, int last){
int mid;
int temp;
if (first == last){
}
else{
if (first +1 == last){
//list of 2 values, swap if needed
if(a.get(first).compareTo(a.get(last)) > 0){
swap(a, first, last);
}
}
else {
//general case
mid = (first + last) / 2;
mergeSort(a, first, mid);
mergeSort(a, mid +1, last);
merge(a, first, mid, last);
}
}
}
private void merge(ArrayList <Comparable> a, int first, int mid, int last)
{
int aPtr = first;
int bPtr = mid + 1;
int cPtr = first;
int total = last - first + 1;
int loop;
boolean doneA = false;
boolean doneB = false;
ArrayList <Comparable> c = new ArrayList <Comparable>(a);
for (loop = 1; loop <= total; loop++){
if (doneA){
c.set(cPtr, a.get(bPtr));
bPtr++;
} else if (doneB){
c.set(cPtr, a.get(aPtr));
aPtr++;
} else if (a.get(aPtr).compareTo(a.get(bPtr)) < 0){
// ok to compare, valid data in each sublist
c.set(cPtr, a.get(aPtr));
aPtr++;
} else {
c.set(cPtr, a.get(bPtr));
bPtr++;
}
cPtr++;
if (aPtr > mid){
doneA = true;
}
if (bPtr > last){
doneB = true;
}
}
ArrayList<Comparable> d = new ArrayList <Comparable>();
for (int i = 0; i < c.size()/2; i++){
d.add(i,c.get(c.size()-1));
}
System.out.println("Sorted list: " + d);
}
public ArrayList <Comparable> fillArray(){//sortstep
Scanner console = new Scanner(System.in);
System.out.println();
System.out.print("How many numbers do you wish to generate? ");
int numInts = console.nextInt();
ArrayList <Comparable> temp = new ArrayList<Comparable>();
System.out.print("Largest integer to generate? ");
int largestInt = console.nextInt();
Random randGen = new Random();
for (int loop = 0; loop < numInts; loop++){
temp.add(randGen.nextInt(largestInt) + 1);
}
return temp;
}
public void swap(ArrayList <Comparable> list, int a, int b){
Comparable c = list.get(a);
list.set(a, list.get(b));
list.set(b, c);
}
}
// End of Recursive merge //
import java.util.ArrayList;
public class merge_recursive_Driver {
public static void main(String[] args){
merge_recursive s = new merge_recursive();
ArrayList standard = s.fillArray();
System.out.println("Standard: " + standard);
int first = (int) standard.get(0);
int last = (int) standard.get(standard.size() -1);
s.mergeSort(standard, first, last);
}
}
// End of Driver //
Output:
How many numbers do you wish to generate? 100
Largest integer to generate? 100
Standard: [81, 4, 23, 2, 88, 70, 64, 74, 1, 16, 16, 11, 24, 88, 28, 89,
52, 5, 86, 73, 89, 95, 69, 15, 58, 34, 80, 63, 96, 11, 63, 92, 95, 71,
87, 76, 94, 87, 27, 23, 69, 47, 87, 55, 14, 90, 9, 61, 13, 39, 56, 55,
19, 20, 85, 93, 6, 8, 90, 9, 26, 99, 41, 11, 60, 22, 30, 46, 52, 20, 1,
23, 2, 37, 10, 19, 89, 16, 43, 12, 47, 52, 28, 13, 10, 41, 46, 91, 49,
62, 66, 17, 87, 69, 47, 58, 45, 38, 83, 31]
Exception in thread "main" java.lang.StackOverflowError
at merge_recursive.mergeSort(merge_recursive.java:11)
at merge_recursive.mergeSort(merge_recursive.java:24)
at merge_recursive.mergeSort(merge_recursive.java:24)
at merge_recursive.mergeSort(merge_recursive.java:24)
at merge_recursive.mergeSort(merge_recursive.java:24)
at merge_recursive.mergeSort(merge_recursive.java:24)
and so on and so on.
"first" and "last" are array indexes, not array values. Replace:
int first = (int) standard.get(0);
int last = (int) standard.get(standard.size() -1);
with
int first = 0;
int last = standard.size() -1;
Don't mind the long code the column methods are repetitive, anyways, the assignment below is bingo and i have put together a check for the first player (player two is essentially copy and paste),and i can't seem to figure out why my check isn't working. Is it the 2D array, or the methods? could someone please explain to me in detail what's the problem and what i need to do to fix it?
package bingo;
import static java.rmi.Naming.list;
import java.util.ArrayList;
import java.util.Collections;
import static java.util.Collections.list;
import java.util.List;
import java.util.Scanner;
public class Bingo {
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
int[] random = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75};
int x = 0, get = 0, gameCount = 0, gameCount2 = 0;
boolean turn = true, gameOver = true;
int[][] cardofzeros = new int[5][5];
int[] column1 = new int[5];
int[] column2 = new int[5];
int[] column3 = new int[5];
int[] column4 = new int[5];
int[] column5 = new int[5];
int[][] card1 = new int[][]{column1(column1), column2(column2), column3(column3), column4(column4), column5(column5)};
System.out.println("");
do {
System.out.println("Enter a positive integer to begin BINGO!/GO AGAIN");
get = input.nextInt();
if (get > 0) {
do {
List l = new ArrayList();
for (int i : random) {
l.add(i);
}
Collections.shuffle(l);
for (int i = 0; i < 2; i++) {
System.out.println(l.get(i));
x = i;
}
System.out.println(" ");
for (int i = 0; i < card1.length; i++) {
for (int j = 0; j < card1.length; j++) {
cardofzeros(cardofzeros, card1, x, j, i, turn,gameCount);
turn=false;
}
}
//System.out.println(random);
} while (turn == true);
} else {
}
} while (gameCount < 5 || gameCount2 < 5);
if (gameCount == 5) {
System.out.println("PLAYER 1 WINS");
} else {
System.out.println("PLAYER 2 WINS");
}
}
public static int[] column1(int[] column1) {
int[] colm = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
List l = new ArrayList();
for (int i : colm) {
l.add(i);
}
Collections.shuffle(l);
for (int i = 0; i < 5; i++) {
System.out.print("|" + l.get(i));
}
System.out.println("");
return column1;
}
public static int[] column2(int[] column2) {
int[] colm2 = {16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
List l = new ArrayList();
for (int i : colm2) {
l.add(i);
}
Collections.shuffle(l);
for (int i = 0; i < 5; i++) {
System.out.print("|" + l.get(i));
}
System.out.println("");
return column2;
}
public static int[] column3(int[] column3) {
int[] colm3 = {31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45};
List l = new ArrayList();
for (int i : colm3) {
l.add(i);
}
Collections.shuffle(l);
for (int i = 0; i < 5; i++) {
System.out.print("|" + l.get(i));
}
System.out.println("");
return column3;
}
public static int[] column4(int[] column4) {
int[] colm4 = {46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60};
List l = new ArrayList();
for (int i : colm4) {
l.add(i);
}
Collections.shuffle(l);
for (int i = 0; i < 5; i++) {
System.out.print("|" + l.get(i));
}
System.out.println("");
return column4;
}
public static int[] column5(int[] column5) {
int[] colm2 = {61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75};
List l = new ArrayList();
for (int i : colm2) {
l.add(i);
}
Collections.shuffle(l);
for (int i = 0; i < 5; i++) {
System.out.print("|" + l.get(i));
}
System.out.println("");
return column5;
}
public static int cardofzeros(int[][] cardofzeros, int[][] card1, int x, int j, int i, boolean turn,int gameCount) {
if (card1[i][j] == x) {
cardofzeros[i][j] = 1;
System.out.println(x + "filled");
turn = false;
} else {
turn = false;
}
horzcheck(card1, cardofzeros, gameCount);
return gameCount;
}
public static int numBingo(int x) {
return x;
}
public static int horzcheck(int[][] card1, int[][] cardofzeros, int gameCount) {
for (int j = 0; j < card1.length; j++) {
for (int i = 0; i < card1.length; i++) {
if (cardofzeros[i][j] == 1) {
gameCount++;
} else {
}
}
}
return gameCount;
}
}
I am trying to generate a random tour from the graph. I am using the adjacency-list method.
There is a problem with the vertices. When I add a vertex to a particular list, the vertex gets added to all the lists in the graph. I do not understand why! Here's the code:
public static void main(String[] args) {
defaultData(6);
}
public static void defaultData(int n) {
Integer costs[] = { 26, 95, 38, 74, 80, 73, 73, 92, 22, 97, 13, 81, 41,
17, 4, 2, 47, 54, 21, 68, 78, 4, 77, 3, 66, 55, 99, 42, 62, 39, 8, 36,
53, 74, 26, 8, 42, 66, 30, 58, 69, 14, 49, 39, 85, 98, 72, 3, 18, 99,
96, 66, 64, 36, 17, 44, 70, 0, 8, 14, 62, 41, 84, 59, 94, 27, 5, 27,
96, 10, 15, 52, 43, 20, 2, 86, 45, 43, 32, 17, 49, 92, 9, 15, 6, 49,
72, 7, 51, 21, 2, 26, 63, 82, 98, 48, 21, 96, 16 };
ArrayList<ArrayList<Integer>> costGraph = new ArrayList<>();
ArrayList<ArrayList<Integer>> completeGraph = new ArrayList<>();
Random rand = new Random(System.currentTimeMillis());
int costIndex = 0;
for (int i = 0; i <= n; i++) {
ArrayList<Integer> cost = new ArrayList<>();
ArrayList<Integer> edge = new ArrayList<>();
for (int j = 0; j <= n; j++) {
if (i == j) {
continue;
}
edge.add(j);
cost.add(costs[costIndex]);
costIndex++;
}
completeGraph.add(edge);
costGraph.add(cost);
}
System.out.println(completeGraph);
ArrayList<ArrayList<Integer>> dummyGraph =
(ArrayList<ArrayList<Integer>>)completeGraph.clone();
ArrayList<ArrayList<Integer>> randomTour = new ArrayList<>();
ArrayList<Integer> dummyList = new ArrayList<>();
for (int i = 0; i <= n; i++) {
randomTour.add(dummyList);
}
System.out.println(dummyGraph);
int edgeCount = 0;
Integer row = rand.nextInt(n);
Integer start = row;
while(edgeCount <= n-1){
//dummyList = dummyGraph.get(row);
// To keep the bounds of the random equal
// to the new reduced size of the lists in the graph
Integer col = dummyGraph.get(row).get(rand.nextInt(n-edgeCount));
randomTour.get(row).add(col);
System.out.println(row);
System.out.println(randomTour);
edgeCount++;
for(int k = 0; k < n; k++)
dummyGraph.get(k).remove(row);
row = col;
}
randomTour.get(row).add(start);
System.out.println(randomTour);
}
I would be very grateful for a timely response. Thanks in advance!
You don't want to do this:
for (int i = 0; i <= n; i++) {
randomTour.add(dummyList);
}
It keeps adding the same reference lots of times, so all the ArrayLists in the ArrayList are actually the same object.
Instead you want to do this:
for (int i = 0; i <= n; i++) {
randomTour.add(new ArrayList<Integer>());
}
That way the ArrayList instances in the ArrayList are all different.
I hope this answer was timely enough!
this is my quick sort code on Java. I've tried this code for 3 different types of lists;
Random list
Sorted List
Reversed sorted list
It's working for only sorted list, but for sorted list it prints;
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 66, 68, 64, 69, 62, 70, 60, 71, 58, 72, 56, 73, 54, 74, 52, 75, 50, 76, 48, 77, 46, 78, 44, 79, 42, 80, 40, 81, 38, 82, 36, 83, 34, 84, 32, 85, 30, 86, 28, 87, 26, 88, 24, 89, 22, 90, 20, 91, 18, 92, 16, 93, 14, 94, 12, 95, 10, 96, 8, 97, 6, 98, 4, 99, 2, 100
For the random list it changes.
I couldn't solve that problem, can anyone help?
Thanks in advance.
class QuickSort {
public int Partition(int[] numbers, int left, int right){
int pivot = numbers[left];
while (true)
{
while (numbers[left] < pivot)
left++;
while (numbers[right] > pivot)
right--;
if (left < right)
{
int temp = numbers[right];
numbers[right] = numbers[left];
numbers[left] = temp;
left++;
right--;
}
else
{
return right;
}
}
}
public void QuickSort(int[] arr, int left, int right){
// For Recusrion
if(left < right)
{
int pivot = Partition(arr, left, right);
if(pivot > 1)
QuickSort(arr, left, pivot - 1);
if(pivot + 1 < right)
QuickSort(arr, pivot + 1, right);
}
}
////////////////////////////////////////////////////////////
public void printArray(int[] arr){
for (int i = 0; i < arr.length; i++){
if (i==arr.length-1)
System.out.print(arr[i]);
else
System.out.print(arr[i]+", ");
}
System.out.println();
}
public int[] fillArraySorted(int len){
int[] result=new int[len];
int counter = 1;
for(int i=0; i<len; i++){
result[i]=counter;
counter++;
}
return result;
}
public int[] fillArrayRan(int len){
int[] result=new int[len];
Random r = new Random();
for(int i=0; i<len; i++){
result[i]=r.nextInt(100)+1;
}
return result;
}
public int[] fillArraySortedReversed(int len){
int[] result=new int[len];
int counter = len;
for(int i=0; i<len; i++){
result[i]=counter;
counter--;
}
return result;
}
}
}
So I am going through some of the common sorting algorithms and have written this:
Code:
public void insertionSort() {
int key;
int i;
for ( int j = 1; j < this.a.length; j++ ) {
key = a[j];
i = j;
while ( i > 0 && a[i-1] > key ) {
a[i] = a[i-1];
i = i - 1;
}
a[i] = key;
}
}
Result:
jan#janspc:~/Dropbox/programming/java/algorithms$ javac sort.java
jan#janspc:~/Dropbox/programming/java/algorithms$ java Test
49, 68, 60, 14, 70, 8, 83, 96, 29, 7, 92, 35, 17, 84, 31, 62, 48, 95, 16, 22, 31, 97, 72, 55, 88, 63, 1, 63, 96, 32, 74, 15, 92, 77, 50, 13, 12, 36, 90, 93, 20, 15, 67, 88, 23, 31, 95, 90, 86, 65, 35, 27, 60, 4, 90, 11, 22, 97, 65, 88, 23, 1, 25, 21, 9, 81, 87, 56, 2, 4, 63, 52, 55, 86, 62, 30, 55, 64, 19, 10, 45, 92, 87, 43, 39, 95, 20, 43, 3, 30, 74, 64, 4, 90, 91, 93, 94, 44, 87, 21,
49, 1, 1, 2, 3, 4, 4, 4, 7, 8, 9, 10, 11, 12, 13, 14, 15, 15, 16, 17, 19, 20, 20, 21, 21, 22, 22, 23, 23, 25, 27, 29, 30, 30, 31, 31, 31, 32, 35, 35, 36, 39, 43, 43, 44, 45, 48, 50, 52, 55, 55, 55, 56, 60, 60, 62, 62, 63, 63, 63, 64, 64, 65, 65, 67, 68, 70, 72, 74, 74, 77, 81, 83, 84, 86, 86, 87, 87, 87, 88, 88, 88, 90, 90, 90, 90, 91, 92, 92, 92, 93, 93, 94, 95, 95, 95, 96, 96, 97, 97,
Execution took: 110628 nano sek?
As you can see from testing, the first value is not affected by sort. What's wrong with my algorithm?
In the first iteration, the while loop doesn't execute, because i < 0. In the next iteration, the while loop doesn't run because i == 0.
You should probably use while (i >= 0 && a[i] > key) (not tested though)
You need >= here:
while ( i >= 0 && a[i] > key ){
Without this change it never compares the following elements against the first one.
while ( i > 0 && a[i] > key ){ should be while ( i >= 0 && a[i] > key ){
Best luck...
In Insertion Sort we need to check each and every element from array and compare with other element getting more accurate result. The problem in your code in while loop
we need line
while ( i >= 0 && a[i] > key )
Here is another implementation of insertion sort in java
public class InsertionSort {
static int intArray[] = { 1000, 1, 100, 101, 15 };
public static void doSort() {
for (int outer = 1; outer < intArray.length; outer++) {
for(int inner=outer;inner>0; inner--){
if(intArray[inner]<intArray[inner-1]){
int temp=intArray[inner];
intArray[inner]=intArray[inner-1];
intArray[inner-1]=temp;
continue;
}
}
}
}
public static void printArray() {
for (int i = 0; i < intArray.length; i++) {
System.out.print(" " + intArray[i]);
}
}
public static void main(String args[]) {
System.out.print("Array Before Sorting->");
printArray();
doSort();
System.out.print("\nArray After Sorting ->");
printArray();
}
}
Detailed explanation of the code and/or the algortithm can be seen at -
http://www.javabrahman.com/algorithms-in-java/insertion-sort-in-java/
here is a very easy implementation of insertion sort
package insertion_sort;
public class insertion_sort {
public static void main(String ar[]) {
int[] a = {24, 13, 9, 64, 7, 23, 34, 47, 87, 9, 37, 1992};
insertion(a,12);
}
public static void insertion(int[] a, int n) {
for (int i = 1; i <= n - 1; i++) {
int value = a[i];
int hole = i;
while (hole > 0 && a[hole - 1] > value) {
a[hole] = a[hole - 1];
hole = hole - 1;
}
a[hole] = value;
}
print(a);
}
public static void print(int[] A) {
for (int i = 0; i < A.length; i++) {
System.out.print(A[i] + " ");
}
System.out.println();
}
}
Below is the pseudo code of Insertion sort(taken from CLR "Introduction to algorithms" book).
Insertion-Sort(A)
for (j=2 to A.length)
key = A[j]>
i = j-1
while i>0 && A[i]>key
A[i+1] = A[i]
i = i-1
A[i+1] = key
Your code is almost similar to above pseudo code. All you need is to change initialization of int j=0 to int j=1 in for loop:
for ( int j = 1; j < this.a.length; j++ ) {
key = a[j];
i = j - 1;
while ( i > 0 && a[i] > key ) {
a[i + 1] = a[i];
i = i - 1;
}
a[i+1] = key;
}
Corrected code:
public void insertionSort() {
int key;
int i;
for ( int j = 1; j < this.a.length; j++ ) {
key = a[j];
i = j;
while ( i > 0 && a[i-1] > key ) {
a[i] = a[i-1];
i = i - 1;
}
a[i] = key;
}
}
public static int[] insrtSort(int array[]){
for(int i=0 ; i<array.length-1;i++){
int value=array[i+1],k=i;
while(value<array[k] ){
array[k+1]=array[k];
if(k!=0)
k--;
else
array[0]=value;
}
if(k!=0)
array[k+1]=value;
}
return array;
}
This is the same code but as a method that can be passed an array of ints to sort.
public static int[] insertionSort(int[] array) {
int key;
int i;
for ( int j = 1; j < array.length; j++ ) {
key = array[j];
i = j;
while ( i > 0 && array[i-1] < key ) {
array[i] = array[i-1];
i = i - 1;
}
array[i] = key;
}
return array;
}