In this example, i want to add the contents of the two arrays, but I am not able to get how should the input be given. For example, in this case, "invalid assignment operator" error is showing up, for the line int[] a = new int[1,2]. I want to know how to call the function addarr, using the arrays a and b.
public class arradd {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = new int[1,2];
int[] b = new int[3,4];
new arradd.addarr(a,b);
}
public void addarr(int[] arr1, int[] arr2){
int total = 0;
for(int i = 0; i < arr1.length; i++){
total += arr1[i];
}
for(int i = 0; i < arr2.length; i++){
total += arr2[i];
}
System.out.println(total);
}
}
One of your problems why the "invalid assignment operator" error is showing up is because you can't do this in java
int[] a =new int[1,2]; // will give you a compiler error.
the parameter inside the square bracket actually means the size of the array.
int[] a=new int[2];
here 2 means the size of the array 'a'.
if you want to declare the contents for the array, do this
int[] a=new int[2]{1,2};
this is exactly what you need ...where the value inside the square bracket tells the compiler the size of the array 'a' and the values inside the curly braces tells the contents what those are the two contents of the given array 'a'.
And you dont need new operator to or the class name itself to call a method within the class. Just do a
addarr(a,b);
to call the function and your function would be invoked.
You are not declaring your arrays properly, try this way:
int[] a = new int[]{1, 2};
int[] b = new int[]{3,4};
Related
Its part of codes.In the first part of code i wrote objects of class.This is fine i havent problem.My problem is on the second part of code
Paper[] pin = new Paper[N]; //N are given by keyboard .
for (int i=0; i < N, i++) //until the lenght N
{
pin[i] = new Paper();
}
pin[0].setpencil(3); // the number 3 i random chose it from my mind,i have done get and set methods
pin[0].getpencil(3);
i want to do it not for class and this is the second part of code down.If i want to create objects not using class this is right way?
int[] pin = new int[N];
for (int i=0; i < N, i++)
{
pin[i] = new int();
}
pin[0].setpencil(3);
pin[0].getpencil(3);
If you want to use an array of integers, just use int[N].
int[] pin = new int[N];
That's it. Java initializes primitives for you to 0 (zero) or false for boolean. You do not need a for loop to set the contents of the array, unless you want some value other than zero.
For example, to initialize all values of your array to 42:
int[] pin = new int[N];
for (int i=0; i < N, i++)
{
pin[i] = 42;
}
To read and write this array, you cannot use methods. Just treat the array deference like any other variable.
pin[0] = 3; // pin[0].setpencil(3);
System.out.println( pin[0] ); // pin[0].getpencil(3); prints "3"
However, classes must work differently (or at least they do in Java). If you have an object array instead of primitives:
Class Paper {
void getpencil(int n) {}
void setpencil(int n) {}
}
and an array
Paper[] myPaper = new Paper[N];
You must access this through methods; you cannot use the form above which is only for primitives.
myPaper[0].setpencil(3);
myPaper[0].getpencil(3);
I have to generate prime number. For this reason I have to initialize all array elements to -1. From c/c++, I came to know I have to initialize them by using a for loop. Please see the code below
public static void main(String[] args){
final int SIZE = 1000;
int[] intArray = new int[SIZE];
Arrays.fill(intArray, -1);
for(int i=0; i<SIZE; i++){
intArray[i] = -1;
}
for(int i = 0; i<SIZE; i++){
System.out.println(intArray[i]);
}
}
In java is there any better way to do this?
Well, Arrays.fill(intArray, -1); already fills your array, so there's no need for the redundant for loop following that statement.
You can also just remove your final int SIZE, and say int[] intArray = new int[1000];. When you need to get the length/size of the array, you can just say intArray.length.
You can only use Arrays.fill(int[] a, int val) method like this -
Arrays.fill(intArray, -1);
The Arrays.fill() populates the array - intArray with the specified value val. So you don't need to initialize the intArray using the for loop.
And one more thing, in c++ it is also possible to initialize array with some value like this, without using the for loop. See here
I am trying to add integers into an int array, but Eclipse says:
cannot invoke add(int) on the array type int[]
Which is completely illogical to me. I also tried addElement() and addInt(), however they don't work either.
public static void main(String[] args) {
int[] num = new int[args.length];
for (String s : args){
int neki = Integer.parseInt(s);
num.add(neki);
}
To add an element to an array you need to use the format:
array[index] = element;
Where array is the array you declared, index is the position where the element will be stored, and element is the item you want to store in the array.
In your code, you'd want to do something like this:
int[] num = new int[args.length];
for (int i = 0; i < args.length; i++) {
int neki = Integer.parseInt(args[i]);
num[i] = neki;
}
The add() method is available for Collections like List and Set. You could use it if you were using an ArrayList (see the documentation), for example:
List<Integer> num = new ArrayList<>();
for (String s : args) {
int neki = Integer.parseInt(s);
num.add(neki);
}
An array doesn't have an add method. You assign a value to an element of the array with num[i]=value;.
public static void main(String[] args) {
int[] num = new int[args.length];
for (int i=0; i < num.length; i++){
int neki = Integer.parseInt(args[i]);
num[i]=neki;
}
}
An array has a fixed length. You cannot 'add' to it. You define at the start how long it will be.
int[] num = new int[5];
This creates an array of integers which has 5 'buckets'. Each bucket contains 1 integer. To begin with these will all be 0.
num[0] = 1;
num[1] = 2;
The two lines above set the first and second values of the array to 1 and 2. Now your array looks like this:
[1,2,0,0,0]
As you can see you set values in it, you don't add them to the end.
If you want to be able to create a list of numbers which you add to, you should use ArrayList.
You cannot use the add method on an array in Java.
To add things to the array do it like this
public static void main(String[] args) {
int[] num = new int[args.length];
for (int i = 0; i < args.length; i++){
int neki = Integer.parseInt(s);
num[i] = neki;
}
If you really want to use an add() method, then consider using an ArrayList<Integer> instead. This has several advantages - for instance it isn't restricted to a maximum size set upon creation. You can keep adding elements indefinitely. However it isn't quite as fast as an array, so if you really want performance stick with the array. Also it requires you to use Integer object instead of primitive int types, which can cause problems.
ArrayList Example
public static void main(String[] args) {
ArrayList<Integer> num = new ArrayList<Integer>();
for (String s : args){
Integer neki = new Integer(Integer.parseInt(s));
num.add(s);
}
Arrays are different than ArrayLists, on which you could call add. You'll need an index first. Declare i before the for loop. Then you can use an array access expression to assign the element to the array.
num[i] = s;
i++;
you have an array of int which is a primitive type, primitive type doesn't have the method add. You should look for Collections.
org.apache.commons.lang.ArrayUtils can do this
num = (int []) ArrayUtils.add(num, 12); // builds new array with 12 appended
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 7 years ago.
This is in java. I tried looking for / googling for an answer to this question here and couldn't find a sufficient answer, so I apologize if this is a duplicate. I am confused as to the rules behind methods changing the parameter that you input. For example, consider the following example
public static void messWithArray(int[] array)
{
array[0] = 100;
array[0] += 5;
// make new array
int[] array2 = new int[10];
for(int i = 0; i < 10; i++)
{
array2[i] = 5+i;
}
array = array2
}
public static void main(String[] args)
{
int[] a = new int[10]
for(int i=0; i < 10; i++)
{
a[i] = i*10
}
messWithArray(a);
System.out.println(a[0]);
System.out.println(a[1]);
This prints that a[0] is 105 and a[1] is 10, so making array[0] equal to 100 and adding 5 to it in the messWithArray method had an effect. However, assigning array = array2 didn't do anything (since a[1] is 10). I also tried messing with an int but couldn't get it to work.
I would like to know more specifically/clearly the logic behind whether or not a method will change attributes of an array.
In Java, method parameters are always passed by value. However, all objects (which includes arrays, even arrays of primitives) are referred to by a pointer, which allows modification of the object through its methods (or via array access for arrays), and it's the pointer that's passed by value.
You can never re-seat what the parameter referred to in the caller, but you can always mutate something mutable.
public void someMethod(int param1, List<String> param2, double[] param3) {
// In here, I can change what 'param1', 'param2', or 'param3' refer to:
param1 = 3;
// but this didn't change the value from whoever called me.
// Doing "param2 = new ArrayList<>();" similarly wouldn't affect the caller.
// The pointer to the list was simply copied.
param2.add("Hello");
// However, that ^ modified the list through the pointer.
// Same is true for arrays:
param3[0] = 0.0; // modified the original array
param3 = new double[3]; // changed 'param3' to refer to a new array
}
I can explain it to you from memory point of view. array is like a pointer pointing to a specific block of memory (let say a memory address as a Long value). When you pass it to memory you pass this pointer to the function. array[5] is like that point + 5 meaning the fifth cell o memory after where array shows. When you do array[5]=100 you actually change the value of a cell of memory which points the fifth cell after where array shows. so you actually change a value inside array. but array2 is a pointer to another cell of the memory. So when you do array = array2 you just change the pointer to new location. So since the pointer is copied via method call, the original pointer outside the function keeps the same and you still see the elements of array
class Arrays
{
public static void messWithArray(int[] array)
{
array[0] = 100;
array[0] += 5;
// make new array
int[] array2 = new int[10];
for(int i = 0; i < 10; i++)
{
array2[i] = 5+i;
System.out.println(array2[i]);
}
array = array2;
System.out.println("CHANGED VALUE --->"+array[0]);
}
public static void main(String[] args)
{
int[] a = new int[10];
for(int i=0; i < 10; i++)
{
a[i] = i*10;
}
messWithArray(a);
System.out.println(a[0]);
System.out.println(a[1]);
}
}
You should return the updated array from your messWithArray() function, this would update the array values.
I am new to the concept of arrays in java, and as part of an assignment, I am required to create a 10 row by 3 column integer array and populate it with a for loop. I have the following.
public class arrays{
int arr[][][] = new int [10][10][10];
for(int i=0;i<arr.length;++i)
{
for(int j=0;j<arr[i].length;++j)
{
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
am I on the right track?
You are actually declaring a 3-dimensional array.
You should instead be declaring int[][] arr = new int[10][3];.
You can then assign values as follows: arr[i][j] = 42;
Note also that when instantiated, the initial values in the array are 0.
The way of creating array in java is simple and I see you know that but for your problem applicable array might be different. Try that one
int arr[][] = new int [10][3];
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
arr[i][j] = your_integer_value;
}
}