How to convert integer in to base -2 using JAVA [duplicate] - java

This question already has answers here:
Integer to binary array
(11 answers)
Closed 7 years ago.
If given int array A is [1,0,0,1,1] =9 A[i]*(-2)^i (^ power).
How to write a java program get the int array sequence for give integer.

public class A {
public static void main(String[] args) {
int [] arr = {1,0,0,1,1};
for (int i = 0; i < arr.length; i++) {
arr[i]=9*arr[i]*(int)Math.pow(-2,i);
}
} System.out.println(Arrays.toString(arr));
}

Related

My program for counting sort always shows this error- Index 9 out of bounds for length 9 [duplicate]

This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 3 months ago.
I have been writing the below mentioned code for the problem and it always shows me the error that my index is out of bounds for my length. I have tried printing the size and it is comparitively very large.
import java.util.*;
class Main {
public static void countingSort(int numbers[]) {
// int n = numbers.length - 2;
int largest=Integer.MIN_VALUE;
for (int i=0;i<numbers.length;i++){
largest=Math.max(largest,numbers[i]);
}
int count[]=new int[largest+1];
System.out.println(count.length+"= length\n");
for (int i=0;i<count.length;i++)
count[numbers[i]]++;
int j=0;
for (int i=0;i<count.length;i++){
while(count[i]>0){
numbers[j]=i;
j++;
count[i]--;
}
}
for(int i=0;i<numbers.length;i++)
System.out.println(numbers[i]);
}
public static void main(String args[]) {
int numbers[] ={5,8,7,19,25,4,2,3,1};
countingSort(numbers);
}
It is because your variable "numbers" only holds 9 values.
for (int i=0;i<count.length;i++)
count[numbers[i]]++;
This for loop will iterate depending on the size of your variable "count". In your case, the variable "count" will hold 26 values. It throws an error when the variable "numbers" is being accessed with an index beyond its size.

Is this a optimal way of initializing an array with 1 through 5? [duplicate]

This question already has answers here:
Fill arrays with ranges of numbers
(7 answers)
Closed 2 years ago.
The usage of i - 1 concerns me the most. Should i use another variable for the indexing or is this fine?
public class MainClass {
public static void main(String[] args) {
int[] array = new int[5];
for(int i = 1; i<=array.length; i++) {
array[i-1] = i;
}
for(int x: array)
System.out.println(x);
}
}
Technically, it doesn't matter. However, you may do
for (int i = 0; i < array.length;)
array[i] = ++i;
to generate the same output:
1
2
3
4
5

converting the binary to decimal when binary is stored in an array [duplicate]

This question already has answers here:
How to convert binary string value to decimal
(15 answers)
Closed 5 years ago.
this is the code I have written but it gives incorrect answers and I can't figure out why
int decimal = 0;
int power= 0;
for(int i=0; i<binary.length; i++)
{
int tmp = binary[i]%10;
decimal+=tmp*Math.pow(2,power);
power++
}
System.out.println(decimal);
For arbitrary sized byte[] you can use BigInteger.
public void test(String[] args) {
byte[] b = new byte[] {1,2,3,4,5};
BigInteger bi = new BigInteger(b);
System.out.println(bi.toString(10));
}
If your array is an int[] then you can convert it - see How to convert int[] to byte[].

Separating numbers of a string array to a integer array [duplicate]

This question already has answers here:
split int value into separate digits
(8 answers)
Closed 6 years ago.
So I have a String array that has a number like "1234567898765" and I want to convert it to an int and assign each number to each index of an array of integers.
For example, array[i]= "1234567898765" and I want to do arrayOfIntegers[0] = 1 , arrayOfIntegers[1] = 2, arrayOfIntegers[2] = 3, ect... How can I do that?
public class StringTest {
public static void main(String args[])
{
String input = "123456789765";
int [] arrayOfInts = new int [input.length() ];
for (int i = 0; i < input.length(); i++ ) {
arrayOfInts[i] = Character.getNumericValue(input.charAt(i));
}
}
}

How to create an array with both int and char [duplicate]

This question already has answers here:
How do I declare and initialize an array in Java?
(31 answers)
Closed 8 years ago.
I'm trying to create a single array that contains the numbers 1-9 and the characters A-F. The array should look like: 1 2 3 4 5 6 7 8 9 A B C D E F. I don't know how to set the array up and would appreciate any advice.
Try this following piece of code,This will work for you,However you must keep in mind that An array can contain only a single type of value as said by #MadProgrammer in comments
import java.util.*;
public class MyClass
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
char[] arr = new char[16];
/*taking input from the keyboard*/
for(int i = 0; i < 16; i++)
{
arr[i] = sc.next().charAt(0);
}
/*displaying the contents of the array*/
for(int i = 0; i < 16; i++)
{
System.out.println(arr[i] + ",");
}
}
}
In this code the numbers 0-9,which you are taking as input are still inserted as characters.

Categories

Resources