I found this snippet online that made a array_intersect like function in java using integer.
Sample:
int[] intersect(int[] arr1, int[] arr2) {
int count = 0;
for(int a = 0; a < arr1.length; a++) {
for(int b = 0; b < arr2.length; b++) {
if(arr1[a] == arr2[b]) {
count++;
break;
}
}
}
int[] result = new int[count];
count = 0;
for(int a = 0; a < arr1.length; a++) {
for(int b = 0; b < arr2.length; b++) {
if(arr1[a] == arr2[b]) {
result[count++] = arr1[a];
break;
}
}
}
return result;
}
int[] arr1 = new int[] {10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
95, 85, 75, 65, 55, 45, 35, 25, 15, 05,
10, 15, 20, 25, 30, 35, 40, 45, 50, 55};
int[] arr2 = new int[] {15, 25, 35, 45, 55,
12, 22, 32, 43, 52,
15, 25, 35, 45, 55};
int[] p1 = this.unique(arr1);
int[] p2 = this.unique(arr2);
int[] intersectResults = this.intersect(arr1, arr2);
for(int a = 0; a < intersectResults.length; a++) {
System.out.print(intersectResults[a] + " ");
}
But when i changed it to:
String[] intersect(String[] a_yourname, String[] a_crushname) {
int count = 0;
for(int a = 0; a < a_yourname.length; a++) {
for(int b = 0; b < a_crushname.length; b++) {
if(a_yourname[a] == a_crushname[b]) {
count++;
break;
}
}
}
String[] result = new String[count];
count = 0;
for(int a = 0; a < a_yourname.length; a++) {
for(int b = 0; b < a_crushname.length; b++) {
if(a_yourname[a] == a_crushname[b]) {
result[count++] = a_yourname[a];
break;
}
}
}
return result;
}
String[] flames = this.intersect(a_yourname, a_crushname);
//String[] p23 = this.unique(arr2);
System.out.println("heheh" +flames.length);
for(int a = 0; a < flames.length; a++) {
System.out.print(flames[a] + " ");
}
Can someone explain to me what I did wrong here? I'm really not familiar with Java.
a_yourname and a_crushname are both string arrays.
Code:
String[] a = new String[] { "aa", "bb", "cc" };
String[] b = new String[] { "bb", "cc", "dd" };
Set<String> setA = new HashSet<String>(Arrays.asList(a));
List<String> listB = Arrays.asList(b);
setA.retainAll(listB);
System.out.println(setA);
You can see the results here:
http://ideone.com/tKg5xa
If you really want the result as an array:
String[] out = setA.toArray(new String[setA.size()]);
Again there's no need to use an array if all you want is to iterate through the result. Google "Java for each" or pick up a Java book.
Related
here fundememtal,database are two subjects. same element's on both
arrays are for the same student. ( fundemental[0],database[0]--> this
is for the student 1 ) . i need to find highest(high rank) to lowest
rank students from this program.i declare a sort method and pass the
total array and create that array total as ascending.just check it out my attachment photo.
i need to find this rank
here is my code.if anybody have unclear, please ask me.
import java.util.*;
class Remove{
public static void main(String args[]){
int [] fundemental={54,34,35,65,87,37};
int database[]={67,56,45,57,78,89};
int[] total=new int[database.length];
for (int i = 0; i < database.length; i++){
total[i]=fundemental[i]+database[i];
}
int [] arrayTot=sort(total);
int index=0;
for (int i = 0; i < total.length; i++){
for (int j = 0; j < total.length; j++){
if(total[i]==(fundemental[j]+database[j]))
index=i;
}
}
}
public static int[] sort(int[]total){
for (int i = total.length; i >0; i--){
int min=total[0];
int index=0;
for (int j =1; j < i; j++)
{
if(total[j]<min){
min=total[j];
index=j;
}
}
total[index]=total[i-1];
total[i-1]=min;
}
return total;
}
}
if anybody has an another idea for find highest rank to lower rand from
student's two subject, please code me.
You can use insertion sort to the small array size and swap all arrays
public static void main(String[] args) {
int[] fundamentals = { 54, 34, 35, 65, 87, 37 };
int database[] = { 67, 56, 45, 57, 78, 89 };
int len = database.length;
int[] total = new int[len];
for (int i = 0; i < len; i++) {
total[i] = fundamentals[i] + database[i];
}
for (int i = 1; i < len; i++) {
for (int j = i; j > 0 && total[j] > total[j - 1]; j--) {
swap(total, fundamentals, database, j, j - 1);
}
}
}
static void swap(int[] total, int[] fundamentals, int[] database, int i, int j) {
int temp = total[j];
total[j] = total[i];
total[i] = temp;
temp = fundamentals[j];
fundamentals[j] = fundamentals[i];
fundamentals[i] = temp;
temp = database[i - 1];
database[i - 1] = database[i];
database[i] = temp;
}
, output
[165, 126, 122, 121, 90, 80]
[87, 37, 65, 54, 34, 35]
[78, 89, 57, 67, 56, 45]
I am implementing Selection Sort Algorithm in java using ArrayList.
Algorithm I implemented is correct but I am not getting valid output.
can anyone help me please if I am going wrong with this Arraylist.
code :
import java.util.*;
public class SelectionSort {
public static void main(String[] args) {
ArrayList <Integer> array = new ArrayList<Integer>();
array.add(50);
array.add(30);
array.add(10);
array.add(60);
array.add(40);
System.out.println(array);
selsort(array);
}
private static ArrayList<Integer> selsort(ArrayList<Integer> array){
int i = 0;
int imin = 0;
int len = array.size();
for (i = 0;i <len-1; i++){
imin = i;
for (int j = i+1; j<len; j++) {
if ((Integer) (array.get(j)) < (Integer) (array.get(imin))){
imin = j;
}
Collections.swap(array,i,j);
}
}
System.out.println(array);
return array;
}
}
output :
[50, 30, 10, 60, 40] //before
[40, 60, 10, 30, 50] //after
You are swapping the elements with the wrong indices in the wrong place.
The correct swap is i with imin.
The correct place is outside the inner loop:
private static ArrayList<Integer> selsort(ArrayList<Integer> array){
int i = 0;
int len = array.size();
for (i = 0; i < len - 1; i++) {
int imin = i;
for (int j = i + 1; j < len; j++) {
if (array.get(j) < array.get(imin)) {
imin = j;
}
}
Collections.swap(array,i,imin);
}
System.out.println(array);
return array;
}
[50, 30, 10, 60, 40]
[10, 30, 40, 50, 60]
Input array of type integer: [24, 53, 20, 35, 34, 64, 14, 12, 21]
after a recursive function, it should give: [53, 35, 21, 24, 20, 34, 64, 14, 12]
the odd numbers places before the even numbers.
Experiencing overflow error in the code:
public int[] seperator(int[] arr)
{
int[] newArr = new int[arr.length] ;
int i = 0;
int j = arr.length-1;
int x = 0;
if(i == arr.length-1 && j == 0 && x == arr.length-1)
{
return newArr;
}
else if(arr[x] % 2 != 0)
{
newArr[i] = arr[x];
i++;
}
else
{
newArr[j] = arr[x];
j--;
}
x++;
return seperator(newArr);
}
Maybe you can try to use ArrayLists and implement this code:
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class oddeven {
public static void main(String[] args) {
List<Integer> l = new ArrayList<Integer>();
Random rd = new Random();
for (int i=0; i<10;i++){
l.add(rd.nextInt(10));
}
List<Integer> lsep = new ArrayList<Integer>(seperator(l));
System.out.println(l);
System.out.println(lsep);
}
public static List<Integer> seperator(List<Integer> l) {
List<Integer> sep = new ArrayList<Integer>();
int i = 0;
int even=0, odd =0;
int len = l.size();
for(i=0; i<len; i++){
if(l.get(i) % 2 == 1) {
odd = l.get(i);
sep.add(odd);
}
}
for(i=0; i<len; i++){
if(l.get(i) % 2 == 0) {
even = l.get(i);
sep.add(even);
}
}
return sep;
}
}
Example,
For a given input :[8, 9, 9, 2, 9, 5, 8, 3, 10, 6]
It will return: [9, 9, 9, 5, 3, 8, 2, 8, 10, 6]
public class Main {
public static void main(String[] arg) {
int[] ar = { 24, 53, 20, 35, 34, 64, 14, 12, 21 };
new Main().swap(ar, 0);
for (int i = 0; i < ar.length; i++) {
System.out.print(ar[i]+" ");
}
}
void swap(int[] ar, int i) {
boolean cont = true; // continue or break checking for odd numbers
if (ar[i] % 2 == 0) {
int j = i;
while (j < ar.length - 1 && ar[j] % 2 == 0) {
j++;
}
int temp = ar[j];
if (j == ar.length - 1) {
cont = false;
}
while (i < j) { // move even numbers forward
ar[j] = ar[j - 1];
j--;
}
ar[i] = temp;
}
if (++i < ar.length && cont)
swap(ar, i); // recursive call
}
}
I have to output the array with a maximum of 4 array values per line, but I can't figure out how to convert it to a 2 dimensional array. After the dashes is where I am having trouble. If I don't output it as a 2D array, how else would I restrict it to have only 4 values per line?
public class arrayExampleB{
public static void main(String[] args){
int[] x = {22, 12, 28, 4, 30, 59, 17, 82, 1, 99, 47, 2, 8, 20, 80};
System.out.print("Pre-Swapped Array Set (linear): {");
for(int i=0; i<=x.length-1; i++){
if(i<x.length-1){
System.out.print(x[i] + ", ");
}
else{System.out.print(x[i]);}
}
System.out.print("}");
int y = x.length-1;
int temp = x[y];
x[y] = x[1];
x[1] = temp;
int z = x.length-2;
int temp2 = x[z];
x[z] = x[0];
x[0] = temp2;
System.out.print("\nPost-Swapped Array Set (linear): {");
for(int i=0; i<=x.length-1; i++){
if(i<x.length-1){
System.out.print(x[i] + ", ");
}
else{System.out.print(x[i]);}
}
System.out.print("}");
//-------------------------------------------------------------
int d = (x.length / 4) + (x.length % 4);
int i = 0;
int j = 0;
int[][] t = new int[i][j];
System.out.print("\nPre-Swapped Array Set (2D): {");
for(i=0; i <= 4; i++){
for(j=0; j < d; j++){
System.out.print(t[i][j] + " ");
}
System.out.println();
}
System.out.print("}");
}
}
To output a 1d array as 2d with a max of 4 values on a line, use this code:
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
if ((i+1) % 4 == 0)
System.out.println();
}
int[] x = {22, 12, 28, 4, 30, 59, 17, 82, 1, 99, 47, 2, 8, 20, 80};
int[][] t = new int[4][4];
// populate 2D
int k = 0
for(i=0; i <= t.length; i++){
for(j=0; j < t[i].length; j++){
t[i][j] = x[k];
k++l
}
}
// print
for(i=0; i <= t.length; i++){
System.out.print("{");
for(j=0; j < t[i].length; j++){
System.out.print(t[i][j]);
}
System.out.println("}");
}
Without taking too close a look on your code: To output a one dimensional array on several lines on the console consider this:
int[] x = {22, 12, 28, 4, 30, 59, 17, 82, 1, 99, 47, 2, 8, 20, 80};
for(int i = 0; i < x.length; i++)
{
System.out.print(x[i] + ' ');
if( (i+1) % 4 == 0)
System.out.print('\n');
}
What I'm trying to do is print the largest number within a two dimensional array and it's index location. I'm able to find the largest number, but I can't seem to figure out how to print it's index location. Anyway, here's what I have so far:
public static void main(String[] args) {
int[][] arr = {{4, 44, 5, 7, 63, 1}, {7, 88, 31, 95, 9, 6}, {88, 99, 6, 5, 77, 4}};
double max = arr[0][0];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if (arr[i][j] > max) {
max = arr[i][j];
}
}
}
System.out.println(max);
System.out.println(i + j); //No idea what I should be doing here, just trying out everything I can think of
Right now, you should consistently get 2 * arr.length as the final value. That isn't what you are probably looking for. It looks like you want to know the coordinates for the max value. To do this, you'll need to cache the values of the indexes and then use them later:
public static void main(String[] args) {
int[][] arr = {{4, 44, 5, 7, 63, 1}, {7, 88, 31, 95, 9, 6}, {88, 99, 6, 5, 77, 4}};
int tmpI = 0;
int tmpJ = 0;
double max = arr[0][0];
// there are some changes here. in addition to the caching
for (int i = 0; i < arr.length; i++) {
int[] inner = arr[i];
// caches inner variable so that it does not have to be looked up
// as often, and it also tests based on the inner loop's length in
// case the inner loop has a different length from the outer loop.
for (int j = 0; j < inner.length; j++) {
if (inner[j] > max) {
max = inner[j];
// store the coordinates of max
tmpI = i; tmpJ = j;
}
}
}
System.out.println(max);
// convert to string before outputting:
System.out.println("The (x,y) is: ("+tmpI+","+tmpJ+")");
Be careful with your array dimensions! The second for-statement most of you have is wrong. It should go to up to arr[i].length:
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] > max) {
max = arr[i][j];
tmpI = i; tmpJ = j;
}
}
}
Store i, j whenever you update max.
This would be if you wanted a single index into a flatten array:
public static void main (String[] args) throws java.lang.Exception
{
int[][] arr = {{4, 44, 5, 7, 63, 1}, {7, 88, 31, 95, 9, 6}, {88, 99, 6, 5, 77, 4}};
int[] flattened = new int[6*3]; // based off above
int maxIndex = 0;
double max = arr[0][0];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
flattened[i + j] = arr[i][j];
if (arr[i][j] > max) {
max = arr[i][j];
maxIndex = i+j;
}
}
}
System.out.println(max);
System.out.println(flattened [maxIndex]);
}
int[][] arr = {{4, 44, 5, 7, 63, 1}, {7, 88, 31, 95, 9, 6}, {88, 99, 6, 5, 77, 4}};
int max = arr[0][0];
int maxI = 0, maxJ = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if (arr[i][j] > max) {
max = arr[i][j];
maxI = i;
maxJ = j;
}
}
}
System.out.println(max);
System.out.println(maxI + "," + maxJ);
You've got a two-dimensional array, therefore you need to know both indexes. Adding them together won't do because you lose which-is-which. How about this:
System.out.println("[" + i + "][" + j + "]");
//C++ code
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> b;
vector<int> c;
int Func(int a[][10],int n)
{
int max;
max=a[0][0];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[i][j]>max)
{
max=a[i][j];
b.push_back(i);
c.push_back(j);
}
}
}
b.push_back(0);
c.push_back(0);
return max;
}
void display(int a[][10],int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
}
int main()
{
int a[10][10],n;
cin>>n;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<endl;
display(a,n);
cout<<endl;
cout<<Func(a,n)<<" is the greatest "<<endl;
if(b.size()==1&&c.size()==1)
{
cout<<"Location is (1,1)"<<endl;
}
else
{
b.erase(b.end() - 1);
c.erase(c.end() - 1);
cout<<"Location is "<<"("<<b.back()+1<<","<<c.back()+1<<")"<<endl;
}
return 0;
}
You're just adding the indices i and j together and then printing it to the screen. Since you're running throug the entire loop it's just going to be equal to 2*arr.length-2. What you need to do is store the values of i and j when you encounter a new max value.
For example:
int[][] arr = {{4, 44, 5, 7, 63, 1}, {7, 88, 31, 95, 9, 6}, {88, 99, 6, 5, 77, 4}};
int max = arr[0][0]; //dunno why you made it double when you're dealing with integers
int max_row=0;
int max_column=0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if (arr[i][j] > max) {
max = arr[i][j];
max_row=i;
max_column=j;
}
}
System.out.println("The max is: "+max+" at index ["+max_row+"]["+max_column+"]");
Don't sure that you implement effective algorithm, but why you just don't save indices i,j in another variables when you set max.
This is very simple.
if (arr[i][j] > max) {
max = arr[i][j];
maxX = i;
maxY = j;
}
FYI If you want look at "insertion sorting" algorithms if you want better implementation.