How to convert 5 1D arrays to a 2D array - java

So i have to create a bingo board which i have done so, don't mind the long code it's repetitive for the first column method. So i wanna convert these 5 1D arrays to 1 2D array so i can check for a bingo. Could someone explain to me in detail how i would convert these 5 arrays to 1 2D array, or if it's possible to check 5 1D arrays.
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;
public class Bingo {
public static void main(String[] args) {
int[][] card1 = 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];
column1(column1);
System.out.println("");
column2(column2);
System.out.println("");
column3(column3);
System.out.println("");
column4(column4);
System.out.println("");
column5(column5);
System.out.println("");
cardofzeros(card1);
for (int i = 0; i < card1.length; i++) {
for (int j = 0; i < card1.length; i++) {
}
}
}
public static void 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));
}
}
public static void 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));
}
}
public static void 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));
}
}
public static void 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));
}
}
public static void 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));
}
}
public static void cardofzeros(int[][] card1) {
for (int i = 0; i < 5; i++) {
for (int j = 0; i < 5; i++) {
card1[i][j]=0;
System.out.print("|"+card1[i][j]);
}
}
}
}

int[] first = null;
int[] second = null;
int[] third = null;
int[] fourth = null;
int[] fifth = null;
int[][] arr2d = new int[][]{first , second , third , fourth , fifth};

Related

returning a sub-array of size n, assume the size of n is between 1 and bestTime.length, and return with the lowest sub arrays

int[] bestTime = {50, 73, 72, 75, 71, 56, 61, 60, 62, 68, 70, 50, 70};
assume if n = 6, expected return = {50, 50, 56, 60, 61, 62}
this is what i have so far, i know there are lots of mistakes. any suggestions is much appreciated.
public static int[] bestRun(int n) {
int[] best = bestTime[0];
for(int i = 0; i <= bestTime.length; i++ ) {
if(bestTime[i] <= best) {
best = bestTime[i];
best++;
} return best;
}
if(best.length == n) {
return best;
}
return null;
}
Build an IntStream of your bestTime array, sort them, limit using n, convert to array and return:
public static int[] bestRun(int n) {
return IntStream.of(bestTime).sorted().limit(n).toArray();
}
You can do the task also using classic for loops. But then you need to implement the sorting yourself. Something like below should give you a how this can be accomplished:
static int[] bestTime = {50, 73, 72, 75, 71, 56, 61, 60, 62, 68, 70, 50, 70};
public static void main(String args[]) throws IOException{
int[] best = bestRun(6);
System.out.println(Arrays.toString(best));
}
public static int[] bestRun(int n) {
//copy your bestTime array
int[] copy = new int[bestTime.length];
for(int i = 0; i < copy.length; i++){
copy[i] = bestTime[i];
}
//sort copy
for (int i = 0; i < copy.length; i++) {
for (int j = i+1; j < copy.length; j++) {
int temp = 0;
if(copy[i] > copy[j]) {
temp = copy[i];
copy[i] = copy[j];
copy[j] = temp;
}
}
}
//fill your result array
int[] result = new int[n];
for(int i = 0; i < n; i++){
result[i] = copy[i];
}
return result;
}

distrubuting people to groups evenly based on userinput

ReadFile randomGenerate = new ReadFile();
Input userNum = new Input();
String classnum = userNum.fileToSelect();
int lineNumber = randomGenerate.lineCounter(classnum);
//number of students in classnum.txt
ArrayList<String> people = randomGenerate.readPeople();
//a string of names(first word on each row)
int userPut = userNum.numInput();
int maxNum = lineNumber/userPut;
Right not, I am reading off a txt file - the first word of each row which is the name of a person. I want to distribute this ArrayList of people into even groups, with the number of groups based on userinput.
int[] numbers = RandomNumbersWithoutRepetition(0, lineNumber, lineNumber);
//an array of numbers IN ORDER
int[] randomNumbers = RandomizeArray(numbers);
//shuffles the array
I generated a set of numbers in order, and then shuffled them, which works fine.
But, my method of grouping people has been based off ifelse conditions which don't work so well. So is there an efficient method to put this ArrayList of names into user-desired groups?
I have x number of students.
I have n number of groups.
I'm assuming there's no other criteria for dividing students into groups.
Basically, you divide the number of groups into the number of students. Yes, you have to deal with a remainder. But it's straightforward.
Here are the results of a test. I created 53 students and shuffled them. I then divided the students into 4 groups. I manually formatted the output to fit in the answer. Group 1 has 14 students, while the remaining groups have 13 students.
Students: 42, 27, 5, 26, 32, 30, 44, 10, 17, 29, 40, 52,
47, 38, 49, 18, 46, 24, 34, 12, 13, 53, 35, 20, 1,
2, 41, 23, 43, 28, 8, 11, 50, 37, 9, 7, 48, 3, 33,
25, 31, 15, 22, 21, 14, 45, 36, 16, 51, 19, 4, 6, 39
Group 1: 42, 27, 5, 26, 32, 30, 44, 10, 17, 29, 40, 52, 47, 38
Group 2: 49, 18, 46, 24, 34, 12, 13, 53, 35, 20, 1, 2, 41
Group 3: 23, 43, 28, 8, 11, 50, 37, 9, 7, 48, 3, 33, 25
Group 4: 31, 15, 22, 21, 14, 45, 36, 16, 51, 19, 4, 6, 39
And here's the code. The groupStudents and sortStudents methods are the methods that do the work.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class StudentGroups {
public static void main(String[] args) {
int numberOfGroups = 4;
int numberOfStudents = 53;
StudentGroups sg = new StudentGroups(numberOfStudents);
sg.printStudents();
List<List<Integer>> groups = sg.groupStudents(
numberOfGroups);
sg.printGroups(groups);
}
private List<Integer> students;
public StudentGroups(int numberOfStudents) {
students = new ArrayList<>(numberOfStudents);
for (int i = 0; i < numberOfStudents; i++) {
students.add(i + 1);
}
Collections.shuffle(students);
}
public void printStudents() {
System.out.print("Students: ");
for (int i = 0; i < students.size(); i++) {
System.out.print(students.get(i));
if (i < (students.size() - 1)) {
System.out.print(", ");
}
}
System.out.println();
}
public List<List<Integer>> groupStudents(int groups) {
List<List<Integer>> output = new ArrayList<>();
int size = students.size();
int group = size / groups;
int remainder = size % groups;
sortStudents(output, size, group, remainder);
return output;
}
private void sortStudents(List<List<Integer>> output,
int size, int group, int remainder) {
List<Integer> list = new ArrayList<>();
int count = 0;
for (int i = 0; i < size; i++) {
list.add(students.get(i));
if (count == 0 && remainder > 0) {
if (group > 0) {
list.add(students.get(++i));
}
remainder--;
}
if (++count >= group) {
addList(output, list);
list = new ArrayList<>();
count = 0;
}
}
addList(output, list);
}
private void addList(List<List<Integer>> output,
List<Integer> list) {
if (list.size() > 0) {
output.add(list);
}
}
public void printGroups(List<List<Integer>> groups) {
for (int j = 0; j < groups.size(); j++) {
System.out.print("Group ");
System.out.print(j + 1);
System.out.print(": ");
List<Integer> students = groups.get(j);
for (int i = 0; i < students.size(); i++) {
System.out.print(students.get(i));
if (i < (students.size() - 1)) {
System.out.print(", ");
}
}
System.out.println();
}
}
}

How to print common elements in 2 arrays without sorted output in Java

Given two arrays, find the common elements in them.
Example: [1,45,33,23,22,45,233,21], [5,23,45,0,9,23,1,9] => Output: [1,45, 23]
import java.io.*;
import java.util.*;
class Mycode {
public static void main(String args[]) {
int a[] = {1, 45, 33, 23, 22, 45, 233, 21};
int b[] = {5, 23, 45, 0, 9, 45, 1, 9};
Mycode test = new Mycode();
test.testNumber(a, b);
}
void testNumber(int c[], int d[]) {
System.out.println(Arrays.toString(c));
System.out.println(Arrays.toString(d));
Set<Integer> hset = new HashSet<Integer>();
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < d.length; j++) {
if (c[i] == d[j]) {
System.out.println(c[i]);
hset.add(c[i]);
}
}
}
System.out.println(hset);
}
}
Actual Output: [1, 45, 33, 23, 22, 4, 233, 21] [5, 23, 45, 0, 9, 5, 1, 9] =>
[1, 23, 45]
HashSet makes no guarantees of the preservation of the insertion order as indicated in the JavaDoc:
It makes no guarantees as to the iteration order of the set; in
particular, it does not guarantee that the order will remain constant
over time.
So I would prefer using a LinkedHashSet. This Set implementation guarantees the preservation of the insertion order. From the JavaDocs:
This linked list defines the iteration ordering, which is the order in
which elements were inserted into the set (insertion-order)
void testNumber(int c[], int d[]) {
System.out.println(Arrays.toString(c));
System.out.println(Arrays.toString(d));
Set<Integer> hset = new LinkedHashSet<>();
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < d.length; j++) {
if (c[i] == d[j]) {
hset.add(c[i]);
}
}
}
System.out.println(hset);
}
Output:
[1, 45, 23]
You can use a
HashMap
Loop through the first array and add each element with the value false.
Loop through the second and if it's in the hashMap then it's common.
What about something like below? This will be more neat since you don't need to have two for loops, and you don't need to sort it before hand.
public class Mycode {
public static void main(String args[]) {
Integer a[] = {1, 45, 33, 23, 22, 45, 233, 21};
Integer b[] = {5, 23, 45, 0, 9, 45, 1, 9};
Mycode test = new Mycode();
System.out.println(test.testNumber(a, b));
}
public <T> Set<T> testNumber(T [] arra1, T [] arr2){
Set<T> set1 = new HashSet<T>();
Set<T> interset = new HashSet<T>();
Collections.addAll(set1, arra1);
for(T t: arr2){
if(set1.contains(t))
interset.add(t);
}
return interset;
}
}
List<Integer> list = new ArrayList<>(Arrays.asList(array1));
list.retainAll(Arrays.asList(array2));
System.out.println(list);
Solution with time complexity of order N
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
// write your code here
Scanner scn = new Scanner(System.in);
int n1 = scn.nextInt();
int[] arr1 = new int[n1];
for (int i = 0; i < n1; i++) {
arr1[i] = scn.nextInt();
}
int n2 = scn.nextInt();
int[] arr2 = new int[n2];
for (int i = 0; i < n2; i++) {
arr2[i] = scn.nextInt();
}
HashMap < Integer, Integer > freqMap1 = new HashMap < > ();
for (int i = 0; i < n1; i++) {
int ch = arr1[i];
if (freqMap1.containsKey(ch)) {
int f = freqMap1.get(ch);
freqMap1.put(ch, f + 1);
} else {
freqMap1.put(ch, 1);
}
}
for (int i = 0; i < n2; i++) {
int ch = arr2[i];
if (freqMap1.containsKey(ch)) {
System.out.println(ch);
freqMap1.remove(ch);
}
}
}
}

Threadpool with callable

I got struked with the ThreadPool method with callable. I want to find Large number in array as well Frequency of it occurence, so i did everything but it showing error. Anyone can help me. Thank you.
import java.util.concurrent.Callable;
public class CallableMethod im``plements Callable<Integer>{
//#SuppressWarnings("unused")
private int[] num;
public CallableMethod(int [] num){
this.num = num;
}
public Integer call() throws Exception{
int n = num[0];
int frequency = 0;
for(int i=1; i< num.length; i++)
{
if(n < num[i]){
n = num[i];
}
}
for(int i = 1; i< num.length; i++){
if (n == num[i]){
frequency++;
}
}
//System.out.println("Largest Number is : " + num);
//System.out.println("frequency of occurence : " + frequency);
return frequency;
}
}
Above one is my callabe() code and
import java.util.concurrent.*;
import java.util.*;
class ThreadPoolMethod {
// static ExecutorService pool = Executors.newFixedThreadPool(2);
public static void main(String[] args) {
ThreadPoolExecutor pool = (ThreadPoolExecutor) Executors.newFixedThreadPool(2);
int number[] = { 32, 43, 145, 53, 25, 98, 54, 32, 65, 63, 145, 98, 43, 23, 25, 98, 100, 102, 105, 123, 145,
122, 123, 11, 12, 1, 0, 123, 145, 145 };
ArrayList<Future<Integer>> future = new ArrayList<Future<Integer>>();
for (int j = 0; j < number.length; j++) {
Future<Integer> f = pool.submit(new CallableMethod(number));
future.add(f);
}
// create array to store results
int result[] = new int[number.length];
for (int j = 0; j < result.length; j++) {
try {
Future<Integer> f = future.get(j);
result[j] = f.get();
} catch (InterruptedException e) {
} catch (ExecutionException e) {
};
}
System.out.println("The Large Number in array is: " + n);
System.out.println("The : " + frequency);
pool.shutdown();
for(int x : result)
System.out.print(x);
}
}
This one is my ThreadPool. Please I'm struked. I cant call callable work into ThreadPool method.Please help me
Try to use this example on java 8 with Streams, CompletableFuture, ForkJoinPool
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.Collectors;
public class DemoController {
public static void main(String[] args) {
ForkJoinPool forkJoinPool = new ForkJoinPool(2);
int number[] = {32, 43, 145, 53, 25, 98, 54, 32, 65, 63, 145, 98, 43, 23, 25, 98, 100, 102, 105, 123, 145,
122, 123, 11, 12, 1, 0, 123, 145, 145};
List<CompletableFuture<Integer>> future = new ArrayList<>();
for (int j = 0; j < number.length; j++) {
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> func(number), forkJoinPool);
future.add(f);
}
List<Integer> result = future.stream().map(f -> {
try {
return f.get();
} catch (Exception e) {
throw new RuntimeException(e);
}
}).collect(Collectors.toList());
forkJoinPool.shutdown();
result.forEach(System.out::println);
}
private static Integer func(int num[]) {
int n = num[0];
int frequency = 0;
for (int i = 1; i < num.length; i++) {
if (n < num[i]) {
n = num[i];
}
}
for (int i = 1; i < num.length; i++) {
if (n == num[i]) {
frequency++;
}
}
System.out.println("Largest Number is : " + n);
System.out.println("frequency of occurence : " + frequency);
return frequency;
}
}

Searching duplicates in 2 dimensional array

I have 2 dimensional array like :
{2 , 6 , 46, 8 , 7 , 25, 64 , 9 , 10},
{6 , 10, 50, 12, 11, 29, 68 , 13, 14},
{46, 50, 90, 52, 51, 69, 108, 53, 54}
How can I find the duplicate elements like '6', '46' and '50'?
My code finds consecutive duplicates:
for (int i = 0; i < a2.length; i++) {
for (int j = 0; j < a2[i].length; j++) {
cursor = a2[i][j];
if(j + 1 < a2[i].length){
if(cursor == a2[i][j + 1]){
System.out.println(cursor + "has duplicate in this array");
}
}
}
}
Iterate through all elements and save it in a temporary set.
When you encounter a duplicate, the list will contain it.
import java.util.HashSet;
import java.util.HashSet;
public class HelloWorld
{
public static void main(String[] args)
{
int[][] arr = {
{2 , 6 , 46, 8 , 7 , 25, 64 , 9 , 10},
{6 , 10, 50, 12, 11, 29, 68 , 13, 14},
{46, 50, 90, 52, 51, 69, 108, 53, 54}
};
HashSet<Integer> elements = new HashSet<>();
HashSet<Integer> duplicates = new HashSet<>();
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if(elements.contains(arr[i][j])) {
duplicates.add(arr[i][j]);
}
elements.add(arr[i][j]);
}
}
System.out.println(duplicates.toString());
}
}
Output:
[50, 6, 10, 46]
Try this code:-
import java.util.Arrays;
import java.util.List;
public class ArrayTest {
public static void main(String[] args) {
Integer[][] myarray = new Integer[][]{
{ 10, 20, 30, 40 },
{ 50, 77, 60, 70 },
{ 33, 22, 88, 99 },
{ 21, 66, 65, 21 }
};
int i,j;
for(i=0;i<myarray.length;i++)
{
for(j=0;j<myarray.length;j++)
{
int temp= myarray[i][j];
myarray[i][j]=0;
List<Integer> rowvalues = Arrays.asList(Arrays.asList(myarray).get(i));
Boolean b=rowvalues.contains(temp) ;
if(b==true)
{
System.out.println("duplicate at ["+i+"]["+j+"] is: "+temp);
}
myarray[i][j]=temp;
}
}
}
}

Categories

Resources