Copying two arrays into one array - java

The question is to write a program which takes two arrays of 10 integers each, a and b. c is an array with 20 integers. The program should put array a and b into the array c, the first 10 integers of c from array a, the latter 10 from b. Then the program should display c. This is the code i have so far. It prints the values from arrayA into arrayC but i dont know how to print the values from arrayB into arrayC without overriding the information already in arrayC (arrayB).
public class questionTwo
{
public static void main(String[] args)
{
int [] arrayA = {31, 14, 5, 12, 50, 80, 100, 29, 58, 57};
int [] arrayB = {9, 13, 156, 78, 36, 46, 86, 98, 63, 2};
int [] arrayC = new int [20];
for (int i = 0; i < arrayA.length; i++)
arrayC[i] = arrayA[i];
for (int i = 0; i < arrayB.length; i++)
{
int element = 9;
arrayC[element] = arrayB[i];
element++;
}
System.out.println("The values of ArrayC are: ");
for(int val : arrayC)
{
System.out.println(val);
}
}
}

Before explaining how you might fix your current code, I would first suggest you create arrayC using the lengths of arrayA and arrayB (here that has the same practical result, but it has the advantage that you can add more elements to arrayA or arrayB without modifying the code). Next, in real code, I would use System.arraycopy to perform the copies. And use Arrays.toString(int[]) for printing. Like,
int[] arrayA = { 31, 14, 5, 12, 50, 80, 100, 29, 58, 57 };
int[] arrayB = { 9, 13, 156, 78, 36, 46, 86, 98, 63, 2 };
int[] arrayC = new int[arrayA.length + arrayB.length];
System.arraycopy(arrayA, 0, arrayC, 0, arrayA.length);
System.arraycopy(arrayB, 0, arrayC, arrayA.length, arrayB.length);
System.out.println(Arrays.toString(arrayC));
As for your original code, use arrayA.length and i to get the correct offset. Like,
int[] arrayC = new int[arrayA.length + arrayB.length];
for (int i = 0; i < arrayA.length; i++) {
arrayC[i] = arrayA[i];
}
for (int i = 0; i < arrayB.length; i++) {
arrayC[i + arrayA.length] = arrayB[i];
}

You were really close! Your second loop initializes element to 9 every time, while you should only assign it to 10 once at the beginning:
int element = 10;
for (int i = 0; i < arrayB.length; i++)
{
arrayC[element] = arrayB[i];
element++;
}

Make your function dynamic while you need to add more item into your arrays. I did not test yet, but I hope it work.
public class questionTwo
{
public static void main(String[] args)
{
int [] arrayA = {31, 14, 5, 12, 50, 80, 100, 29, 58, 57};
int [] arrayB = {9, 13, 156, 78, 36, 46, 86, 98, 63, 2};
int [] arrayC = new int [arrayA.length + arrayB.length];
for (int i = 0; i < arrayA.length; i++)
arrayC[i] = arrayA[i];
int element = arrayA.length;
for (int i = 0; i < arrayB.length; i++)
{
arrayC[element+i] = arrayB[i];
}
System.out.println("The values of ArrayC are: ");
for(int val : arrayC)
{
System.out.println(val);
}
}
}

The second loop is wrong. You can use the follow code:
int [] arrayA = {31, 14, 5, 12, 50, 80, 100, 29, 58, 57};
int [] arrayB = {9, 13, 156, 78, 36, 46, 86, 98, 63, 2};
int [] arrayC = new int [20];
System.arraycopy(arrayA, 0, arrayC, 0, arrayA.length);
System.arraycopy(arrayB, 0, arrayC, arrayA.length, arrayB.length);
System.out.println("The values of ArrayC are: ");
for(int val : arrayC) {
System.out.println(val);
}

Your second loop is incorrect because you are setting element to 9 within the loop each time; thus, its value is always reset upon beginning the next iteration. You could move it outside the loop to achieve the result you're looking for. Also, I'd recommend constructing arrayC based on the lengths of arrayA and arrayB, that way you can combine array's of any length.
In addition, I'd also recommend moving the code to combine the arrays to a method to keep things clean and reusable for future endeavors.
public class questionTwo {
private static int[] combineArrays(int[] arrayA, int[] arrayB) {
int[] arrayC = new int[arrayA.length + arrayB.length];
int idx = 0;
for (int x : arrayA) {
arrayC[idx] = x;
idx++;
}
for (int x : arrayB) {
arrayC[idx] = x;
idx++;
}
return arrayC;
}
public static void main(String[] args) {
int[] arrayA = {31, 14, 5, 12, 50, 80, 100, 29, 58, 57};
int[] arrayB = {9, 13, 156, 78, 36, 46, 86, 98, 63, 2};
int[] arrayC = combineArrays(arrayA, arrayB);
System.out.println("The values of ArrayC are: ");
for(int val : arrayC) {
System.out.println(val);
}
}
}
Also, it is often considered a poor practice to use Java's primitive arrays. Check out List and related objects for more flexible and full-featured options.

Related

How can I reorder my array based off the first value?

I'm working on a problem that says "Write a function called splay that reorganizes a list based on the first value. The first value is called the splaymaster. This function will rearrange the list such that all of the values before the splaymaster are less than or equal to the splaymaster and all of the values after it are greater than the splaymaster. The function also returns the index where the splaymaster is located after the list is rearranged. For example, if the list is [8, 15, 4, 48, 26, 45, 18, 29, 2, 1], the function will rearrange it to become [2, 1, 4, 8, 26, 45, 18, 29, 48, 15] and return the value 3. You may not sort the list." The problem that I seem to be having is for the example above it complains that the index is out of bounds which is my first problem. The next is if I use another array such as {90, 8, 15, 4, 48, 26, 45, 18, 29, 2, 1} I get {1, 90, 8, 15, 4, 48, 26, 45, 18, 29, 2} this is my output when it's not correct. What am I doing wrong? and How do I fix it?
public static void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static int splay(int[] x) {
int left = 1;
int right = x.length;
int splaymaster = x[0];
while (left < right) {
if (x[left] <= splaymaster) {
left++;
} else {
swap(x, left, right);
}
swap(x, 0, left - 1);
}
return splaymaster;
}
}
int right = x.length;
The index of the last element in an array is x.length - 1, hence the index is out of bounds. Nonetheless, your algorithm seems incorrect. Merely swapping the elements at different locations is not sufficient. If an element is less than the splaymaster then you need to move all the elements up one and insert the smaller element into the array before the splaymaster. I assume there may be other conditions regarding the way to solve the problem but if there are then they are not clear to me from your question, for example it appears that the order of the elements is not important just as long as all elements before the splaymaster are less than or equal to it. I also assume you need to change the array in place, i.e. you are not allowed to use a second array.
Consider the following code:
import java.util.Arrays;
public class SplayTst {
public static int splay(int x[]) {
int index = 0;
int splaymaster = x[0];
for (int i = 1; i < x.length; i++) {
if (x[i] <= splaymaster) {
int temp = x[i];
for (int j = i; --j >= 0;) {
x[j + 1] = x[j];
}
x[0] = temp;
index++;
}
}
return index;
}
public static void main(String[] args) {
int[] test = new int[]{8, 15, 4, 48, 26, 45, 18, 29, 2, 1};
int ndx = splay(test);
System.out.println(Arrays.toString(test));
System.out.println(ndx);
test = new int[]{90, 8, 15, 4, 48, 26, 45, 18, 29, 2, 1};
ndx = splay(test);
System.out.println(Arrays.toString(test));
System.out.println(ndx);
}
}
Running the above code produces the following output:
[1, 2, 4, 8, 15, 48, 26, 45, 18, 29]
3
[1, 2, 29, 18, 45, 26, 48, 4, 15, 8, 90]
10
Alternatively, assuming that you can use any method to solve the problem, consider the following which uses ArrayList and then converts it to an array and then all the array elements are copied to the original array. Note that the single code line for converting ArrayList<Integer> to int[] is taken from the following question:
How to convert an ArrayList containing Integers to primitive int array?
I refer to this line of the below code:
int[] arr = list.stream().mapToInt(i -> i).toArray();
I iterate through the original array. If an element is greater than the splaymaster then it is appended to the ArrayList, otherwise it is inserted as the first element in the ArrayList.
import java.util.ArrayList;
import java.util.Arrays;
public class SplayTst {
public static int splay(int[] x) {
ArrayList<Integer> list = new ArrayList<>();
list.add(x[0]);
int index = 0;
for (int i = 1; i < x.length; i++) {
if (x[i] <= x[0]) {
list.add(0, x[i]);
index++;
}
else {
list.add(x[i]);
}
}
int[] arr = list.stream().mapToInt(i -> i).toArray();
for (int i = 0; i < x.length; i++) {
x[i] = arr[i];
}
return index;
}
public static void main(String[] args) {
int[] test = new int[]{8, 15, 4, 48, 26, 45, 18, 29, 2, 1};
int ndx = splay(test);
System.out.println(Arrays.toString(test));
System.out.println(ndx);
test = new int[]{90, 8, 15, 4, 48, 26, 45, 18, 29, 2, 1};
ndx = splay(test);
System.out.println(Arrays.toString(test));
System.out.println(ndx);
}
}

code for generating none repeating random numbers. Looks fine to me but when i run it gives me the same number

import java.util.Random;
public class Practice_assignment {
public static void main(String[] args){
int[] winning_numbers = {0,0,0,0,0,0,0,0,0,0} ;
//The i++ for the first loop is in the second loop. I was trying to ensure it only goes to
the next value of the loop once a unique value has been gotten.
for (int i=0; i<10;){
int max = 99;
int min = 1;
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
for (int j=0;j<=i;j++){
if (j<i && winning_numbers[j]==randomNum){
break;
}
else if (j==i && i<10){
winning_numbers[i] = randomNum;
System.out.println(winning_numbers[i]+" ");
i++;
}
}
}
}
}
If I understood correctly what you are trying to achieve, I think you could use something like this:
public class RandomNumbers {
public static void main(String[] args) {
int[] winning_numbers = {0,0,0,0,0,0,0,0,0,0} ;
Random random = new Random();
for(int i = 0; i < winning_numbers.length; i++) {
OptionalInt generatedInt = random.ints(1, 0, 100).findFirst(); // generates a stream of 1 number between 0 and 99 and gets the first (and only) one generated
while (contains(winning_numbers, generatedInt)) {
generatedInt = random.ints(1, 0, 100).findFirst(); // while the generated number is already in the array, generate a new one
}
winning_numbers[i] = generatedInt.getAsInt();
}
System.out.println(Arrays.toString(winning_numbers));
}
// this method will check if the given array already contains the generated int
private static boolean contains(int[] arr, OptionalInt generatedInt) {
return Arrays.stream(arr).anyMatch(number -> generatedInt.getAsInt() == number);
}
}
I ran it a couple of times and here are some outputs I generated with this code:
[52, 54, 21, 62, 47, 13, 94, 36, 82, 25]
[35, 37, 16, 81, 22, 71, 17, 94, 56, 8]
[51, 50, 80, 62, 18, 88, 1, 53, 44, 79]
[16, 95, 18, 66, 31, 4, 1, 55, 52, 26]
[4, 11, 65, 68, 22, 76, 95, 67, 35, 92]
[49, 87, 34, 88, 71, 57, 12, 76, 70, 78]
It appears you want to generate an array of winning_numbers. Here is one ways to do it.
create a helper method to look for duplicate numbers and return true if a duplicate is found.
then iterate over the array checking for the current random number and adding it if unique.
note that min, max, and rand should be initialized outside the loop.
Random rand = new Random();
int[] winning_nummbers = new int[10];
int max = 99;
int min = 1;
for (int i = 0; i < 10;) {
int randomNum = rand.nextInt((max - min) + 1) + min;
// if does not contain a duplicate, then assign
if (!contains(winning_numbers, randomNum)) {
winning_numbers[i++] = randomNum;
}
}
System.out.println(Arrays.toString(winning_numbers));
Prints something like this.
[46, 91, 5, 2, 42, 58, 74, 24, 53, 36]
The helper method.
public static boolean contains(int[] array, int v) {
for (int i = 0; i < array.length; i++) {
if (array[i] == v) {
return true;
}
}
return false;
}
If you want to use streams you can do it like so using a Random instance.
stream values form min to max inclusive.
drop duplicates with distinct
limit(10) limits to 10 values.
put them in an array
int[] winning_numbers = rand.ints(min,max+1)
.distinct()
.limit(10)
.toArray();

How to rearrange string after permutation

I'm using the code below to permute a string according to a specific table.
public static void main(String[] args) {
int[] IP = { 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7 };
String text = "00000001 00100011 01000101 01100111 10001001 10101011 11001101 11101111".replace(" ", "");
text = Permute(text,IP);
System.out.println(text);
}
public static String Permute(String text,int [] table )
{
String keys = "0" + text;
StringBuilder sb = new StringBuilder ();
for (int i = 1 ; i <= table.length; i++)
{
sb.append(keys.charAt(table[i-1]));
}
return sb.toString();
}
UPDATED:Any idea to create another method to get the original string back? Something like :
public static String GetoriginalText(String TextafterPermutation,int [] table )
You can use a chararray that you populate according to your table mapping
public static String RePermute(final String text, final int[] table) {
final String keys = text;
char[] chararray = new char[table.length];
for (int i = 0; i < keys.length() && i < table.length; i++) {
chararray[table[i]-1] = keys.charAt(i);
}
return new String(chararray);
}
This code is iterating through your text, and writes the char of the current iteration at the chararray place specified in your table.
Note: There are some problems in your code that i left untouched.
For example I really don't get why you allways put "0" in front of your text.
Also you might want to handle situations where the passed text and table differ in length.
Edit: I removed the part where you add "0" in front of your passed text and instead changed the loop to start at i=0 instead i=1.
You can simplify your algorithm to use array of chars instead of StringBuilder:
public static String permute(String text,int [] table )
{
char[] chars = new char[table.length];
for (int i = 0 ; i < table.length; i++) {
chars[i] = text.charAt(table[i]-1);
}
return new String(chars);
}
After that reverse algorithm is more obvious. You just need to make reverse assignment:
public static String undo(String text,int [] table ) {
char[] chars = new char[table.length];
for (int i = 0; i < table.length; i++)
{
chars[table[i]-1] = text.charAt(i);
}
return new String(chars);
}
In the statement text = Permute(text,IP); you are assigning reference of String object returning from Permute function, so the original reference which text variable was holding is replaced by new reference returned by Permute function.
If you want to keep the original reference which text was holding, just use any other variable like String output = Permute(text,IP);.
You could use a simple program as mentioned below:
public static String dePermute(String text, int[] table){
String keys = text;
System.out.println(table.length);
String[] str = new String[table.length];
for (int i = 1; i <= table.length; i++) {
System.out.println("Coming "+table[i - 1]);
str[table[i - 1]-1] = ""+keys.charAt(i-1);
//sb.append(keys.charAt(table[i - 1]));
}
return Arrays.toString(str).replace("[", "").replace("]", "").replace(",", "").replace(" ", "");
}
You can use the concept explained in this program. I have just reverse the process which you are doing in Permute function.
assuming your permutation table doesn't change you can do this:
public static String GetoriginalText(String TextafterPermutation,int [] table ){
char[] chars=new char[table.length];
for(int i=0;i<table.length;i++){
chars[table[i] - 1] = TextafterPermutation.charAt(i);
}
return new String(chars);
}

Initializing int array

I didn't understand why I1.array[0]=555 is? I only want to change arr[0]
Intar I1 = new Intar(10);
int [] arr = I1.array;
arr[0]=555;
public class Intar {
int length;
int [] array;
public Intar(int lengt){
length=lengt;
array=new int[lengt];
Random ran =new Random();
for(int i=0; i<length; i++){
array[i]= ran.nextInt(100);
**When I use System.out.println before and after arr[0]=555 **
I1.array [11, 57, 77, 74, 50, 62, 1, 11, 23, 27]
arr [11, 57, 77, 74, 50, 62, 1, 11, 23, 27]
After arr[0]=555
I1.array [555, 57, 77, 74, 50, 62, 1, 11, 23, 27]
arr [555, 57, 77, 74, 50, 62, 1, 11, 23, 27]
int [] arr = I1.array; does not copy the array, it just assigns the reference to the l1.array to arr. You could use Arrays.copyOf(int[], int) to make a copy like
int[] arr = Arrays.copyOf(l1.array, l1.array.length);
Intar I1 = new Intar(10);
Creates an Intar object, using the constructor public Intar(int lengt), this then initilizes the fields of the Intar object to be an array of 10 random numbers, and a length = 10.
When you write
int [] arr = I1.array;
You are telling arr to reference the object stored in I1's field called array (the array of 10 randomly generated numbers). So when you then set arr[0]=555 you are also setting I1.array's 0th element to be 555 as well.
What you have to understand is I1.array hold a reference to the array which is stored in the Intar object. And now arr is referencing the same object as I1.array is referencing.
For example:
public class Foo {
int val;
public Foo(int val){
this.val = val;
}
int getVal(){
return this.val;
}
}
Then we can do this:
public static void main(String[] args) {
Foo a,b;
a = new Foo(5);
b = a;
b.val = 5;
System.out.println(a.getVal());
}
5 will be the output as b now references a, and we changed the value of b thus we changed the value of a.

Iterate through array, sum and put results in new array

I have an arrayList filled with integers and I need to iterate through this arrayList, add the numbers up until a threshold is reached, then put the resulting sum into a second arrayList slot, and move back to where I left off in the original arrayList, keep iterating and summing until the threshold is reached and then put that in the second slot, and so on until all 40 original items have been summed and put into a smaller arrayList.
I've thought of using two nested loops but I can't get the two loops to work together.
I'm very new to Java and don't know how to do this. Any suggestions will be helpful.
This is what I have so far:
int threshold = 12;
int sumNum = 0;
int j = 0;
int arr1[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
for (int i = 0; i < arr1.length; i++) {
while (sumNum <= threshold) {
sumNum += arr[j];
j++
}//end while
}//end for
You can actually do this with just one loop. You don't need to move back, just stay where you are and keep going on the sum.
public static ArrayList<Integer> sums(ArrayList<Integer> arr, int threshold){
ArrayList<Integer> sumArr = new ArrayList<Integer>();
int s = 0; //Sum thus far, for the current sum
for(int i : arr){
s += i; //Add this element to the current sum
if(s >= threshold){ //If the current sum has reached/exceeded the threshold
sumArr.add(s); //Add it to the sumArray, reset the sum to 0.
s = 0;
}
}
return sumArr;
}
You can change the input param from ArrayList to int[] or Integer[] without any trouble whatsoever. Hooray for for-each loops!
Code to use the above:
public static void main(String[] args){
ArrayList<Integer> i = new ArrayList<Integer>();
//create arraylist 1..20
for(int x = 1; x <= 20; x++){
i.add(x);
}
System.out.println(sums(i).toString());
}
package com.test;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
int threshold = 12;
int sumNum = 0;
int j = 0;
int arr1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
List myList = new ArrayList();
for (int i = 0 ; i < arr1.length ; i++) {
sumNum += i;
if (sumNum >= threshold) {
myList.add(sumNum);
sumNum = 0;
}
}
for (int a = 0 ; a < myList.size() ; a++) {
System.out.println(myList.get(a));
}
}
}
output
15
13
17
21
12
13
14
How about something like this:
/**
* Sequentially adds numbers found in the source array until the sum >= threshold.
* <p/>
* Stores each threshold sum in a separate array to be returned to the caller
* <p/>
* Note that if the last sequence of numbers to be summed does not meet the threshold,
* no threshold sum will be added to the result array
*
* #param numbersToSum The source number list
* #param threshold The threshold value that determines when to move on to
* the next sequence of numbers to sum
*
* #return An array of the calculated threshold sums
*/
public static Integer[] sumWithThreshold(int[] numbersToSum, int threshold)
{
List<Integer> thresholdSums = new ArrayList<Integer>();
if (numbersToSum != null)
{
int workingSum = 0;
for (int number: numbersToSum)
{
workingSum = workingSum + number;
if (workingSum >= threshold)
{
thresholdSums.add(workingSum);
workingSum = 0;
}
}
}
return thresholdSums.toArray(new Integer[thresholdSums.size()]);
}
public static void main(String[] args)
{
int[] testNumbers =
{
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};
int[] thresholds = {1, 42, 100, 200};
for (int threshold: thresholds)
{
System.out.println("Threshold sums for threshold = " + threshold + ":\n" + Arrays.toString(sumWithThreshold(testNumbers, threshold)));
}
}
That produces the following output:
Threshold sums for threshold = 1:
[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]
Threshold sums for threshold = 42:
[45, 46, 45, 54, 63, 47, 51, 55, 59, 63, 67, 71, 75, 79]
Threshold sums for threshold = 100:
[105, 105, 115, 110, 126, 105, 114]
Threshold sums for threshold = 200:
[210, 225, 231]

Categories