JAVA Dynamic Array [duplicate] - java

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]);}
}
}

Related

Java method to file [duplicate]

This question already has answers here:
How do I create a file and write to it?
(35 answers)
Closed 4 years ago.
I'm writing code that prints you combination of numbers from 1 to n. In console it works perfectly fine, but i need to print it into file.
What should i do?
Code:
public class Permutace {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("your number: ");
int N = sc.nextInt();
int[] sequence = new int[N];
for (int i = 0; i < N; i++) {
sequence[i] = i + 1;
}
permute(sequence, 0);
}
public static void permute(int[] a, int k){
if (k == a.length) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]);
}
System.out.println();
} else {
for (int i = k; i < a.length; i++) {
int temp = a[k];
a[k] = a[i];
a[i] = temp;
permute(a, k + 1);
temp = a[k];
a[k] = a[i];
a[i] = temp;
}
}
}
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("your number: ");
int N = sc.nextInt();
int[] sequence = new int[N];
PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
for (int i = 0; i < N; i++) {
sequence[i] = i + 1;
}
permute(sequence, 0);
writer.close();
}
public static void permute(int[] a, int k){
if (k == a.length) {
for (int i = 0; i < a.length; i++) {
writer.println(a[i]);
}
writer.println();
} else {
for (int i = k; i < a.length; i++) {
int temp = a[k];
a[k] = a[i];
a[i] = temp;
permute(a, k + 1);
temp = a[k];
a[k] = a[i];
a[i] = temp;
}
}
}

Given three integers a, b, and n,output the following series: a+20b,a+20b+21b,......,a+20b+21b+...+2n−1ba+20b,a+20b+21b,......,a+20b+21b+...+2n−1b

Constraints:
0≤t≤500
0≤a,b≤50
1≤n≤15
Sample Input:
2
0 2 10
5 3 5
Sample Output
2 6 14 30 62 126 254 510 1022 2046
8 14 26 50 98
This works in my IDE but when I try it in an online editor in hackerrank.com it throws an exception:
Exception in thread "main" java.util.NoSuchElementException: No line foundat
java.util.Scanner.nextLine(Scanner.java:1585)at
Solution.main(Solution.java:24)
Please explain why this happens.Thanks!
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
int[] a = new int[10];
int[] b = new int[10];
int[] n = new int[10];
int t;
int sum;
StringBuilder sb =new StringBuilder();
Scanner iput = new Scanner(System.in);
t = Integer.parseInt(iput.nextLine());
if (t <= 500) {
for (int i = 0; i < t; i++) {
a[i] = Integer.parseInt(iput.next());
b[i] = Integer.parseInt(iput.next());
n[i] = Integer.parseInt(iput.next());
iput.nextLine();
}
} else
System.out.println("Enter value less than 500");
if (t <= 500) {
for (int i = 0; i < t; i++) {
if (a[i] <= 50 && b[i] <= 50 && n[i] <= 15 && n[i] != 0) {
for (int j=0;j<n[i];j++) {
sum = a[i];
for (int k = j;k >=0; k--) {
sum+=Math.pow(2,k)*b[i];
}
sb=sb.append(Integer.toString(sum)).append(" ");
}
System.out.println(sb);
sb.delete(0,sb.toString().length());
} else
System.out.println("Enter the values within the allowed limits");
}
}
}
}
Remove iput.nextLine(); on line 24 so that no extra reading will happen.
I think the below should work. Because value of T can be up to 500 So the array can be either int[] a = new int[t] or new int[500].
Note if you are using int[t] the get the value of t before defining a,b and n.
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
int[] a = new int[500];
int[] b = new int[500];
int[] n = new int[500];
int t;
int sum;
StringBuilder sb =new StringBuilder();
Scanner iput = new Scanner(System.in);
t = Integer.parseInt(iput.nextLine());
if (t <= 500) {
for (int i = 0; i < t; i++) {
a[i] = Integer.parseInt(iput.next());
b[i] = Integer.parseInt(iput.next());
n[i] = Integer.parseInt(iput.next());
}
} else
System.out.println("Enter value less than 500");
if (t <= 500) {
for (int i = 0; i < t; i++) {
if (a[i] <= 50 && b[i] <= 50 && n[i] <= 15 && n[i] != 0) {
for (int j=0;j<n[i];j++) {
sum = a[i];
for (int k = j;k >=0; k--) {
sum+=Math.pow(2,k)*b[i];
}
sb=sb.append(Integer.toString(sum)).append(" ");
}
System.out.println(sb);
sb.delete(0,sb.toString().length());
} else
System.out.println("Enter the values within the allowed limits");
}
}
}
}
import java.util.Scanner;
public class hackerrank_loop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int j = sc.nextInt();
for (int i = 0; i < j; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
int n = sc.nextInt();
int x = 1;
int o = a;
for (int k = 0; k < n; k++) {
o = o + x * b;
System.out.print(o);
x = x << 1;
System.out.print(' ');
}
System.out.println();
}
}
}
I've solved in this way the problem.
For sure it can be done better.
Scanner in = new Scanner(System.in);
int q=in.nextInt();
int[][] app;
if(q<0 && q>500){
System.out.println("ERROR = Select a valid value o q");
}else{
app= new int[q][3];
for(int i=0;i<q;i++){
app[i][0] = in.nextInt();
app[i][1] = in.nextInt();
app[i][2]= in.nextInt();
}
for (int one=0;one<q;one++){
StringBuffer sb = new StringBuffer();
int a = app[one][0];
int b = app[one][1];
int n = app[one][2];
int sum = a;
for (int two=0; two < n; two++){
sum = sum + b*(int)Math.pow(2D,new Double(two));
sb.append(sum);
sb.append(" ");
}
System.out.println(sb);
}
}
in.close();

JAVA dynamic arrays [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a question and I will provide my code below. I need to target sequences to be as much as positive or negative members of the array. Can anyone tell me what I need to change in my code to do so?
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]);}
}
}
first of all you can simplify this:
if (array1[i] > 0 || array1[i] == 0) {
to
if (array1[i] >= 0) {
second one remove this variables. there are not necessary:
int i, a, b;
in each for loop define the type of i:
for (int i = 0;
next one you dont know the number of positive and negative numbers so I suggest you to use a List instead of arrays:
change
int [] arrayPlus = new int[limit];
int [] arrayMinus = new int[limit];
to
List<Integer> arrayPlus = new ArrayList<>();
List<Integer> arrayMinus = new ArrayList<>();
add elements with:
arrayPlus.add(array1[i]);
arrayMinus.add(array1[i]);
and read elements with:
System.out.println(arrayPlus.get(i));
System.out.println(arrayMinus.get(i));
iterate over the lists with:
for (int i = 0; i < arrayPlus.size(); i++) {
OVERALL:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class DynamicArray {
public static void main(String[] args) {
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 (int i = 0; i < limit; i++) {
array1[i] = s.nextInt();
}
List<Integer> arrayPlus = new ArrayList<>();
List<Integer> arrayMinus = new ArrayList<>();
for (int i = 0; i < limit; i++) {
if (array1[i] >= 0) {
arrayPlus.add(array1[i]);
} else {
arrayMinus.add(array1[i]);
}
}
System.out.println("Positive array numbers");
for (int i = 0; i < arrayPlus.size(); i++) {
System.out.println(arrayPlus.get(i));
}
System.out.println("");
System.out.println("Negative array numbers");
for (int i = 0; i < arrayMinus.size(); i++) {
System.out.println(arrayMinus.get(i));
}
}
}
OVERALL(without Lists)
import java.util.Scanner;
class DynamicArray {
public static void main(String[] args) {
int p=0,n=0;
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 (int i = 0; i < limit; i++) {
int lInput = s.nextInt();
if (lInput >= 0) {
p++;
} else {
n++;
}
array1[i] = lInput;
}
int[] arrayPlus = new int[p];
int[] arrayMinus = new int[n];
int indexPlus = 0;
int indexMinus = 0;
for (int i = 0; i < limit; i++) {
if (array1[i] >= 0) {
arrayPlus[indexPlus] = array1[i];
indexPlus++;
} else {
arrayMinus[indexMinus] = array1[i];
indexMinus++;
}
}
System.out.println("Positive array numbers");
for (int i = 0; i < arrayPlus.length; i++) {
System.out.println(arrayPlus[i]);
}
System.out.println("");
System.out.println("Negative array numbers");
for (int i = 0; i < arrayMinus.length; i++) {
System.out.println(arrayMinus[i]);
}
}
}

java arrays, executing positive and negative numbers

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]
}

Debugging Mergesort Implementation

Okay, I know this question doesn't show research effort, but I've been going through this code so many times and I couldn't figure out was I was doing wrong. I know there are many Mergesort implementation examples but I wanted to do it my way. Any help is appreciated, thanks.
import java.util.Scanner;
public class MergeSort
{
public static int[] mergeSort(int[] arr)
{
if (arr.length > 1)
{
int[] arr1 = splitLeft(arr);
int[] arr2 = splitRight(arr);
arr1 = mergeSort(arr1);
arr2 = mergeSort(arr2);
return merge(arr1, arr2);
}
else
return arr;
}
public static int[] splitLeft(int[] arr)
{
int middle = arr.length / 2;
int[] newarr = new int[middle];
for (int i = 0; i < middle; i++)
newarr[i] = arr[i];
return newarr;
}
public static int[] splitRight(int[] arr)
{
int middle = arr.length / 2;
int[] newarr = new int[arr.length - middle];
for (int i = 0; i + middle < arr.length; i++)
newarr[i] = arr[i + middle];
return newarr;
}
public static int[] merge(int[] arr1, int[] arr2)
{
int[] sorted = new int[arr1.length+arr2.length];
int i1 = 0;
int i2 = 0;
int i = 0;
while (i1 < arr1.length && i2 < arr2.length)
{
if (arr1[i1] < arr2[i2])
{
sorted[i] = arr1[i1];
i1++;
}
else
{
sorted[i] = arr2[i2];
i2++;
}
i++;
}
while (i1 < arr1.length)
{
sorted[i] = arr1[i1];
i1++;
i++;
}
while (i2 < arr2.length)
{
sorted[i] = arr1[i2];
i2++;
i++;
}
return sorted;
}
public static int getNum(int x)
{
int num = (int)(Math.random()*x + 1);
return num;
}
public static void printArr(int[] arr)
{
System.out.println();
for (int i = 0; i < arr.length; i++)
System.out.println(arr[i]);
}
static Scanner reader = new Scanner(System.in);
public static void main(String [ ] args)
{
int i;
System.out.println("Type the length of the array");
int n = reader.nextInt();
System.out.println("Type the range of the random numbers generator");
int range = reader.nextInt();
int[]arr = new int[n];
for (i = 0; i < n; i++)
arr[i] = getNum(range);
printArr(arr);
int[] sorted = new int[n];
sorted = mergeSort(arr);
printArr(sorted);
}
}
I think the problem is in your splitRight function. Consider this code:
for (int i = middle; i < arr.length; i++)
newarr[i] = arr[i];
This tries to copy the ith element from arr to the ith position of newarr, but this is incorrect. For example, if the array arr has ten elements, you want to copy element 5 of arr to position 0 of newArr, element 6 of arr to position 1 of newarr, etc.
To fix this, consider trying something like this:
for (int i = 0; i + middle < arr.length; i++)
newarr[i] = arr[i + middle];
Hope this helps!
When you do
for (int i = middle; i < arr.length; i++)
newarr[i] = arr[i];
You are surely asking for positions in the original array and at the same time looking for them in the new array (which happens to be shorter).

Categories