I need to execute positive and negative numbers from an array list. I also need to execute duplicates from array list. I will post my java code and hope someone can tell me why I can't run this code. Is there something missing in it? Thanks in advance.
public static void main(String[] args) {
int i, a, b;
int[] array1 = new int[20];//{12,23, -22, 0, 43,545, -4, -55,43, 12,0, -999, -87
array1[0] = 12;
array1[1] = 23;
array1[2] = -22;
array1[3] = 0;
array1[4] = 43;
array1[5] = 545;
array1[6] = -4;
array1[7] = -55;
array1[8] = 43;
array1[9] = 12;
array1[10] = 0;
array1[11] = -991;
array1[12] = -87;
int[] arrayPlus = new int[20];
int[] arrayMinus = new int[20];
a = b = 0;
for (i = 0; i < 13; i++) {
if (array1 > 0 || array1 == 0) {
arrayPlus[a] = array1;
a++;
} else {
arrayMinus = array1;
b++;
}
}
System.out.println("Positive array numbers");
for (i = 0; i < a; i++) {
System.out.println(arrayPlus);
}
System.out.println("");
System.out.println("Negative array numbers");
for (i = 0; i < b; i++) {
System.out.println(arrayMinus);
}
}
}
You are comparing whole table array1 with integer. You can't do that. You should compare only one element of the array with 0. That mean you should use array1[i] instead.
Try to change this block:
if (array1 > 0 || array1 == 0){
arrayPlus[a] =array1;
...
{arrayMinus =array1;
...
}
for (i = 0; i < a; i++) {
System.out.println(arrayPlus);}
System.out.println("Negative array numbers");
for (i = 0; i < b; i++) {
System.out.println(arrayMinus);}
}
With this:
if (array1[a] > 0 || array1[a] == 0){
arrayPlus[a] =array1[a];
...
{arrayMinus[a] =array1[a];
...
for (i = 0; i < a; i++) {
System.out.println(arrayPlus[a]);}
System.out.println("Negative array numbers");
for (i = 0; i < b; i++) {
System.out.println(arrayMinus[b]);}
}
And for more learn array go in link
Try to instead of code
for (i = 0; i < 13; i++) {
if (array1 > 0 || array1 == 0) {
arrayPlus[a] = array1;
a++;
} else {
arrayMinus = array1;
b++;
}
}
Use following code:
for (i = 0; i < 13; i++) {
if (array1[i] > 0 || array1[i] == 0) {
arrayPlus[a] = array1[i];
a++;
} else {
arrayMinus[b] = array1[i];
b++;
}
}
You should work with elements of arrays (array1[i], arrayMinus[b]) not with whole arrays (array1, arrayMinus). Some problem with code:
for (i = 0; i < a; i++) {
System.out.println(arrayPlus); // use arrayPlus[i]
}
System.out.println("");
System.out.println("Negative array numbers");
for (i = 0; i < b; i++) {
System.out.println(arrayMinus); // use arrayMinus[i]
}
Related
I created this program that allows the user to input 5 numbers for array 1 and 5 numbers for array 2, the idea of the program is to iterate through those arrays and find the matching values for example: user types on input 1 = 1, 2, 3, 4 and 5 and the same for input 2, the lowest matching value is 1 and my program does that, if there is no matching value displays a message that there is no matching values and my program does that. However, if the user inputs something like this on 1 = 3, 4, 5, 7, 2 and input 2 = 9, 12, 8, 7, 15, what my program does in this case, variable min1 on array1 find lowest value which is 2 and variable min2 on array2 find lowest value which is 7 so in theory does not match, but they are asking me to find the lowest MATCHING values so both of them have 7 so it should display 7, I have some code there that select the 7 on each array and display them, now I have to figure out how to add that temporal variable into the displays so it only displays one or the other, as of now it displays 7 and then no matching value, tried adding it to the if statement but couldn't make it
import java.util.Scanner;
public class SmallestArrayItem {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int array1[] = new int[5];
int array2[] = new int[5];
System.out.println("Please enter 5 values for array 1");
for(int i = 0; i < array1.length; i++) {
int userInput = keyboard.nextInt();
array1[i] = userInput;
}
System.out.println("Please enter 5 values for array 2");
for(int i = 0; i < array2.length; i++) {
int userInput = keyboard.nextInt();
array2[i] = userInput;
}
int min1 = array1[0];
for(int index1 = 1; index1 < array1.length; index1++) {
if(array1[index1] < min1) {
min1 = array1[index1];
}
}
int min2 = array2[0];
for(int index2 = 1; index2 < array2.length; index2++) {
if(array2[index2] < min2) {
min2 = array1[index2];
}
}
int tmpval = Integer.MAX_VALUE;
for(int i = 0; i < array1.length; i++){
for(int j = 0; j < array2.length; j++){
if(array1[i] == array2[j]){
// same value
if(tmpval > array1[i]){
tmpval = array1[i];
}
}
}
}
System.out.println(tmpval);
if(min1 == min2) {
System.out.println("The Smalest match in the array is : " + min1);
} else if(min1 != min2) {
System.out.println("There is no smallest matching integer!");
}
}
}
Your code look great, its just a problem with your logic. In your question, you talked about nesting for loops, so you were on the right track.
int[] arr1 = {3,4,5,7,2};
int[] arr2 = {9,12,8,7,15};
int tmpval = Integer.MAX_VALUE;
for(int i = 0; i < arr1.length; i++){
for(int j = 0; j < arr2.length; j++){
if(arr1[i] == arr2[j]){
// same value
if(tmpval > arr1[i]){
tmpval = arr1[i];
}
}
}
}
System.out.println(tmpval);
It's not clear from your description if the matching values have to be at the same position in the two arrays. If we assume that they do then something like this would work:
int[] arr1 = {3,4,5,7,2};
int[] arr2 = {9,12,8,7,15};
int minIdx = -1;
for(int i = 0; i < arr1.length; i++)
{
if(arr1[i] == arr2[i] && (minIdx < 0 || arr1[i] < arr1[minIdx]))
{
minIdx = i;
}
}
if(minIdx < 0)
System.out.println("No match");
else
System.out.println("Min match: " + arr1[minIdx]);
Note how we use a negative index as an indicator that we haven't found a match. The standard trick of using Integer.MAX_VALUE as our starting value isn't really safe as it could be a matching value in the array.
If the matching values can appear at any position in the arrays then you'll need to compare each value in array1 with every value in array2:
for(int i = 0; i < arr1.length; i++)
{
for(int j = 0; j < arr2.length; j++)
{
if(arr1[i] == arr2[j] && (minIdx < 0 || arr1[i] < arr1[minIdx]))
{
minIdx = i;
}
}
}
Thanks for all your help guys, this is the final code:
public class SmallestArrayItem {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int array1[] = new int[5];
int array2[] = new int[5];
System.out.println("Please enter 5 values for array 1");
for(int i = 0; i < array1.length; i++) {
int userInput = keyboard.nextInt();
array1[i] = userInput;
}
System.out.println("Please enter 5 values for array 2");
for(int i = 0; i < array2.length; i++) {
int userInput = keyboard.nextInt();
array2[i] = userInput;
}
int minIdx = -1;
for(int i = 0; i < array1.length; i++)
{
for(int j = 0; j < array2.length; j++)
{
if(array1[i] == array2[j] && (minIdx < 0 || array1[i] < array1[minIdx]))
{
minIdx = i;
}
}
}
if(minIdx < 0)
System.out.println("There is no smallest matching integer!");
else
System.out.println("The Smallest match in the array is : " + array1[minIdx]);
}
}
I am trying to implement a solution to merge two sorted arrays to a single sorted array.
I tried to implement a code but it does not work. The issue that I am seeing here is after my outer for loop reaches the limit of array2.length(), it still gets started from i=0. Can someone please help explain whats wrong in this piece of code?
List<Integer> mergedArray = new ArrayList<>();
int counter = 0;
public List<Integer> getMergedArray(List<Integer> array1, List<Integer> array2) {
for (int i = 0; i < array1.size(); i++) {
for (int j = counter; j < array2.size(); j++) {
if (array1.get(i) > array2.get(j)) {
mergedArray.add(array2.get(j));
counter++;
} else {
mergedArray.add(array1.get(i));
System.out.println("i " +i);
break;
}
}
}
return mergedArray;
}
One problem I see with the code is when the two arrays do not have same length. In either case of (array1.length > array2.length or reverse) the remaining elements of longer array will not be processed. So if you add a more code at end of this the loops to deal with that case, this might work well!
public List<Integer> getMergedArray(List<Integer> array1, List<Integer> array2) {
List<Integer> mergedArray = new ArrayList<>();
int counter = 0, i = 0, j = 0;
for (i = 0; i < array1.size(); i++) {
for (j = counter; j < array2.size(); j++) {
if (array1.get(i) > array2.get(j)) {
mergedArray.add(array2.get(j));
counter++;
} else {
mergedArray.add(array1.get(i));
System.out.println("i " +i);
break;
}
}
}
// More code here
if (j==array2.size()){//copy rest of array1 into mergedArray}
if (i==array1.size()){//copy rest of array2 into mergedArray}
return mergedArray;
}
I think you called the same method twice, without resetting the counter or the previous mergedArray.
You should declare those inside the method.
Additionally, after reaching the end of array1, you should check if there are remaining elements in array2, and vice-versa:
public List<Integer> getMergedArray(List<Integer> array1, List<Integer> array2) {
List<Integer> mergedArray = new ArrayList<>();
int counter = 0;
for (int i = 0; i < array1.size(); i++) {
for (int j = counter; j < array2.size(); j++) {
if (array1.get(i) > array2.get(j)) {
mergedArray.add(array2.get(j));
counter++;
} else {
mergedArray.add(array1.get(i));
System.out.println("i " +i);
break;
}
}
if(counter >= array2.size()) {
mergedArray.add(array1.get(i));
}
}
for (int j = counter; j < array2.size(); j++) {
mergedArray.add(array2.get(j));
}
return mergedArray;
}
I think it works but problem is that you don't handle case when inner array size is equal or bigger than size of outer array. I rewrote your method (in way I think it`s right). Hope you'll find it useful.
public List<Integer> getMergedArray(List<Integer> array1, List<Integer> array2) {
List<Integer> mergedArray = new ArrayList<>();
int index1 = 0;
int index2 = 0;
for (int i = 0; i < array1.size() + array2.size(); i++) {
Integer num1 = null;
Integer num2 = null;
if (index1 < array1.size()) {
num1 = array1.get(index1);
}
if (index2 < array2.size()) {
num2 = array2.get(index2);
}
if (num1 != null && num2 != null) {
if (num1 < num2) {
mergedArray.add(num1);
++index1;
} else {
mergedArray.add(num2);
++index2;
}
} else if (num1 != null) {
mergedArray.add(num1);
++index1;
} else {
mergedArray.add(num2);
++index2;
}
}
return mergedArray;
}
If your array2 has all the element less then array1[i]
in this case your logic also fails because your inner loop condition always fail
you can do this way
int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2) {
if (arry1[i] <= arry2[j]) {
TempArry[k] = arry1[i];
i++;
}
else {
TempArry[k] = arry2[j];
j++;
}
k++;
}
/* Copy remaining elements of array1[] if any */
while (i < n1) {
TempArry[k] = array1[i];
i++;
k++;
}
I have a method that takes 2 attributes which are 2 arrays and merges them in the ascending order. All that's left for me is to figure out how to delete duplicates. Here's the code:
public static int[] mergeArrays(int[] a, int[] b){
int[] c = new int[a.length+b.length];
int aIt = 0;
int bIt = 0;
while(true) {
if(aIt < a.length && bIt < b.length) {
if(a[aIt] == b[bIt]){
c[aIt+bIt] = a[aIt++];
}
else{
c[aIt+bIt] = b[bIt++];
}
} else if(aIt < a.length) {
c[aIt+bIt] = a[aIt++];
} else if(bIt < b.length) {
c[aIt+bIt] = b[bIt++];
} else {
break;
}
}
return c;
}
As you can imagine, this is an assignment, so I'm not supposed to use any external libraries, otherwise this would be a lot easier.
I tried this method, but when I run this code in the console it seems to put it in some never-ending loop which consumes my CPU core entirely until I stop the process:
public static int[] mergeArrays(int[] a, int[] b){
int[] c = new int[a.length+b.length];
int aIt = 0;
int bIt = 0;
int lastVal = 0;
while(true) {
if(c[aIt+bIt] == lastVal){
continue;
}
else{
if(aIt < a.length && bIt < b.length) {
if(a[aIt] == b[bIt]){
c[aIt+bIt] = a[aIt++];
lastVal = c[aIt+bIt];
}
else{
c[aIt+bIt] = b[bIt++];
lastVal = c[aIt+bIt];
}
} else if(aIt < a.length) {
c[aIt+bIt] = a[aIt++];
lastVal = c[aIt+bIt];
} else if(bIt < b.length) {
c[aIt+bIt] = b[bIt++];
lastVal = c[aIt+bIt];
} else {
break;
}
}
}
return c;
}
It seems as though the "continue" keyword is the problem. When I try break in its place, the code executes.
EDIT:
I've added a new array to the mergeArrays method:
public static int[] mergeArrays(int[] a, int[] b)
{
int a_size = a.length;
int b_size = b.length;
int[] c = new int[a_size + b_size];
int[] d = null;
int i = 0 , j = 0, x = -1;
for(; i < a_size && j < b_size;)
{
if(a[i] <= b[j])
{
c[++x] = a[i];
++i;
}
else
{
if(c[x] != b[j])
{
c[++x] = b[j]; // avoid duplicates
}
++j;
}
}
--i; --j;
while(++i < a_size)
{
c[++x] = a[i];
}
while(++j < b_size)
{
c[++x] = b[j];
}
d = new int[uniqueValues(c)];
for(int g=0; g<uniqueValues(c); g++){
d[g] = c[g];
}
return d;
}
Well you can certainly do these in following two steps task :
Sort both the arrays.
static void sortArray(int[] a) {
for (int lastPos = a.length - 1; lastPos >= 0; lastPos--) {
for (int index = 0; index <= lastPos - 1; index++) {
if (a[index] > a[index + 1]) {
int temp = a[index];
a[index] = a[index + 1];
a[index + 1] = temp;
}
}
}
}
Remove duplicates while merging them.
public static int[] mergeArrays(int[] a, int[] b)
{
int a_size = a.length;
int b_size = b.length;
int[] c = new int[a_size + b_size];
int i = 0 , j = 0, x = -1;
for(; i < a_size && j < b_size;)
{
if(a[i] <= b[j])
{
c[++x] = a[i];
++i;
}
else
{
if(c[x] != b[j])
{
c[++x] = b[j]; // avoid duplicates
}
++j;
}
}
--i; --j;
while(++i < a_size)
{
c[++x] = a[i];
}
while(++j < b_size)
{
c[++x] = b[j];
}
return c;
}
Count unique values in newly merged array.
static int uniqueValues(int[] c) {
int uniqueValues = c.length;
for (int i = 0; i < c.length; i++) {
while (c[i] == c[i + 1] && i + 1 < c.length && c[i] > c[i+1] ) {
i++;
uniqueValues--;
}
}
return unique;
}
Reestablish the merged array with unique value count i.e. delete the remaining elements after count finishes.
sortArray(a);
sortArray(b);
int[] c = mergeArrays(a, b);
c = deleteDuplicateEntries(c, uniqueValues(c));
Let me know if you find it useful.
Let the libraries work for you:
(Note, that outside of jshell, you have to terminate each non-empty line with ";")
int[] ai = {0, 1, 15, 16, 27, 84, 49, 4, 5 }
int[] bi = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 4, 5 }
List <Integer> li = new ArrayList<> ()
Arrays.stream (ai).forEach (i -> li.add (new Integer (i)))
Arrays.stream (bi).forEach (i -> li.add (new Integer (i)))
Collections.sort (li)
int [] res = li.stream().distinct().mapToInt (i -> i.intValue()).toArray()
result in jshell:
for (int i: res) System.out.printf (" %d", i);
0 1 2 3 4 5 6 7 8 9 10 11 12 15 16 27 49 84
This question already has answers here:
Variable length (Dynamic) Arrays in Java
(7 answers)
Closed 7 years ago.
I will post my code and hope someone can tell what to change to make a "dynamic array"? The current code works but they told me it is not the correct way and instead of this I need to make a dynamic array. Thanks in advance.
public static void main(String[] args) {
int i,a,b;
int [] array1 = new int[20];//{12,23, -22, 0, 43,545, -4, -55,43, 12,0, -999, -87
array1[0] = 12;
array1[1] = 23;
array1[2] = -22;
array1[3] = 0;
array1[4] = 43;
array1[5] = 545;
array1[6] = -4;
array1[7] = -55;
array1[8] = 43;
array1[9] = 12;
array1[10] = 0;
array1[11] = -999;
array1[12] = -87;
int [] arrayPlus = new int[20];
int [] arrayMinus = new int[20];
a=b=0;
for (i = 0; i < 13; i++) {
if (array1[i] > 0 || array1[i] == 0) {
arrayPlus[a] = array1[i];
a++;
} else {
arrayMinus[b] = array1[i];
b++;
}
}
System.out.println("Positive array numbers");
for (i = 0; i < a; i++) {
System.out.println(arrayPlus[i]);}
System.out.println("");
System.out.println("Negative array numbers");
for (i = 0; i < b; i++) {
System.out.println(arrayMinus[i]);}
}
}
Please check the below code
import java.util.Scanner;
class DynamicArray {
public static void main(String[] args) {
int i,a,b;
System.out.println("Enter the limit of array :" );
Scanner s = new Scanner(System.in);
int limit = s.nextInt();
int [] array1 = new int[limit];//{12,23, -22, 0, 43,545, -4, -55,43, 12,0, -999, -87
System.out.println("Enter the numbers");
for(i=0; i<limit; i++)
{
array1[i] = s.nextInt();
}
int [] arrayPlus = new int[limit];
int [] arrayMinus = new int[limit];
a=b=0;
for (i = 0; i < limit; i++) {
if (array1[i] > 0 || array1[i] == 0) {
arrayPlus[a] = array1[i];
a++;
} else {
arrayMinus[b] = array1[i];
b++;
}
}
System.out.println("Positive array numbers");
for (i = 0; i < a; i++) {
System.out.println(arrayPlus[i]);}
System.out.println("");
System.out.println("Negative array numbers");
for (i = 0; i < b; i++) {
System.out.println(arrayMinus[i]);}
}
}
I have been trying to figure out how to properly print the return of my method.
When the program prints the return of my method, I am giving a nullPointerException error on line 45(the line where i am trying to print the method).
*I did try to make the return to the method static so it is accessible.
How do I initialize the "answer" variable so that i can print it outside of my method?
Thank you in advance
import javax.swing.JOptionPane;
public class ListSortMerge {
static int[]answer;
public static void main(String[] args) {
int v1 = 0, v2 = 0;
for(int c = 0; c <= 1; c++) {
String values = JOptionPane.showInputDialog("How many values would you like to store in list "+(c+1)+"?");
if (c==0) {
v1 = Integer.parseInt(values);
}
else{
v2 = Integer.parseInt(values);
}
}
int[] numbers1 = new int[v1];
int[] numbers2 = new int[v2];
merge(numbers1,numbers2);
int i;
System.out.println("\nList 1 before the sort");
System.out.println("--------------------");
for(i = 0; i < (v1); i++) {
System.out.println(numbers1[i]);
}
System.out.println("\nList 2 before the sort");
System.out.println("--------------------");
for(i = 0; i < (v2); i++) {
System.out.println(numbers2[i]);
}
System.out.println("\nList after the sort");
System.out.println("--------------------");
for(i = 0; i < (v1+v2); i++) {
System.out.println(answer[i]);
}
}
public static int[] merge(int[] a, int[] b) {
int[] answer = new int[a.length + b.length];
for(int c = 0; c < (a.length); c++)
{
String aVal1 = JOptionPane.showInputDialog("Input list 1 value " +(c+1));
a[c] = Integer.parseInt(aVal1);
}
for ( int c = 0; c < (b.length); c++){
String aVal2 = JOptionPane.showInputDialog("Input list 2 value " +(c + 1));
b[c] = Integer.parseInt(aVal2);
}
int i = 0, j = 0, k = 0;
while (i < a.length && j < b.length)
{
if (a[i] < b[j])
answer[k++] = a[i++];
else
answer[k++] = b[j++];
}
while (i < a.length)
answer[k++] = a[i++];
while (j < b.length)
answer[k++] = b[j++];
return answer;
}
}
You have two different answer variables: one is a local variable in the merge function and another is a static field in the class. You never initialize the second one.