How to access an array from the main method? - java

public static int arraylistExample() {
int length [] = new int [10];
length[0] = 2;
length[1] = 3;
length[5] = 8;
return length [1];
}
I have written in the main method this:
System.out.println(length[5]);
Hence, my code looks like this:
import java.util.ArrayList;
public class Javanotes {
public static void main(String[] args) {
System.out.println(length[5]);
}
public static int arraylistExample() {
int length [] = new int [10];
length[0] = 2;
length[1] = 3;
length[5] = 8;
return length [1];
}
}
and I get a "0" what should I do?

1) The array is defined in the method scope of arraylistExample(). It is not visible from main().
2) The array will be empty if you never call the method.
You could change it in this way :
public class Javanotes {
static int length [];
public static void main(String[] args) {
System.out.println(arraylistExample()); // print 3
System.out.println(length[5]); // print 8
}
public static int arraylistExample() {
length = new int [10]; // init the static field
length[0] = 2;
length[1] = 3;
length[5] = 8;
return length [1];
}
}
But note that using static everywhere is generally not the right thing.
You do that for utility classes.

You should declare the function before calling it.
your function arraylistexample is not seen by the main class

Related

Array Method issue

having a problem with my java program. I am a newbie to Java and just can't figure out what is exactly the issue with it. In short I've declared an array and a variable in main, I've created my method call and would like my array be passed into my method with the variable. I would then like the method to take my array and count the number of times my variable "8" occurs, get rid of the 8 out of the array and return a new smaller array back to main. Here is my code below. I feel as if I am just missing one block code any suggestions?
public class Harrison7b
{
public static void main(String [] args)
{
int[] arrayA = {2,4,8,19,32,17,17,18,25,17,8,3,4,8};
int varB = 8;
// Call with the array and variable you need to find.
int[] result = newSmallerArray(arrayA, varB);
for(int x = 0; x < arrayA.length; x++)
{
System.out.print(arrayA[x] + " ");
}
}
public static int[] newSmallerArray( int[] arrayA, int varB)
{
int count = 0;
for(int x = 0; x < arrayA.length; x++)
{
if(arrayA[x] == varB)
{
count++;
}
}
int [] arrayX = new int[arrayA.length - count];
for(int B = 0; B < arrayA.length; B++)
{
if(arrayA[B] != varB)
{
}
}
return arrayX;
}
}
you do not actually need to return the array because when you pass an array to a method you also pass its memory address meaning its the same address that you change so, it will also change the arraysA of main method because you are just changing the values of the same memory adress
import java.util.*;
public class Help
{
public static void main(String[] args)
{
ArrayList<Integer> arraysA = new ArrayList<Integer>();
arraysA.add(Integer.valueOf(2));
arraysA.add(Integer.valueOf(4));
arraysA.add(Integer.valueOf(8));
arraysA.add(Integer.valueOf(19));
arraysA.add(Integer.valueOf(32));
arraysA.add(Integer.valueOf(17));
arraysA.add(Integer.valueOf(17));
arraysA.add(Integer.valueOf(18));
arraysA.add(Integer.valueOf(25));
arraysA.add(Integer.valueOf(17));
arraysA.add(Integer.valueOf(8));
arraysA.add(Integer.valueOf(3));
arraysA.add(Integer.valueOf(4));
arraysA.add(Integer.valueOf(8));
int varB=8;
newSmallerArray(arraysA,varB);
for(Integer i:arraysA)
{
System.out.println(i);
}
}
public static void newSmallerArray(ArrayList<Integer> arraysA,int varB)
{
for(int i=0;i<arraysA.size();++i)
{
if(Integer.valueOf(arraysA.get(i))==varB)
{
arraysA.remove(i);
}
}
}
}
Try this code it will not require for loop:
List<Integer> list = new ArrayList<Integer>(Arrays.asList(arrayA));
list.removeAll(Arrays.asList(8));
arrayA = list.toArray(array);

How to pass a reference array into a setter?

Write a method called setTo5 which is passed a reference to an array of ints and sets the contents of that array to all 5s. This is what I have so far, and it's giving me an error on the second line after public static void main(...);. How to make a better setter?
public class Set5 {
private static int n = 8;
static int[] boop = new int[n];
public static void main(String[] args){
int[] roop = new int[n];
roop.setTo5(5);
}
public void setTo5(int poop){
for(int i = 0; i<n ; i++){
poop = boop[i];
}
}
}
Try something like this:
public class Set5 {
private static int n = 8;
static int[] boop = new int[n];
public static void main(String[] args){
int[] roop = new int[n];
//Create instance of Set5 class to call setter
Set5 set5reference = new Set5();
set5reference.setTo5(roop);
//All the values in roop will now be 5 as set by setter.
}
//change this method to accept array reference like this
public void setTo5(int[] poop){
for(int i = 0; i<n ; i++){
poop[i] = 5;
}
}
}
To fill an array entirely with some value use:
java.util.Arrays.fill(poop, 5)
In your case:
public class Set5 {
private static int n = 8;
//static int[] boop = new int[n]; // unused variable
public static void main(String[] args){
int[] roop = new int[n];
setTo5(roop);
print(roop);
}
public static void setTo5(int[] poop){
java.util.Arrays.fill(poop, 5)
// for(int i = 0; i<poop.length ; i++){
// poop[i]=5;
//}
}
public static void print(int[] poop){
for(int i = 0; i<poop.length ; i++){
System.out.println("array["+i+"] = "+poop[i]);
}
}
}

Array average calculation

Cannot find why my array in the class aint working. Not sure if maybe static has something to do with the problem. I receive alot of errors but i think the main one is "cannot make a static ref to the non-static field rej"
MAIN:
public class Arajmain {
public static void main (String[]args){
System.out.println(Araj.genomsnittet());
}
}
CLASS
public class Araj {
double [] rej = new double[3];
public static double genomsnitt;
rej[0] = 4;
rej[1] = 7;
rej[2] = 9;
public static double genomsnittet(){
genomsnitt = (rej[0] + rej[1] + rej[2])/3;
return genomsnitt;
}
}
public class Araj {
private static double [] rej = new double[3];
static {
rej[0] = 4;
rej[1] = 7;
rej[2] = 9;
}
public static double genomsnittet(){
double genomsnitt = (rej[0] + rej[1] + rej[2])/3;
return genomsnitt;
}
}
That's some piece of ugly code though. You should definetly have a look at a tutorial about Java and OOP.
Add the static modifier to reg:
static double [] rej = new double[3];
Also, you can initialize it using a static initialization block:
static {
rej[0] = 4;
rej[1] = 7;
rej[2] = 9;
}
Or using using a more concise array literal:
static double [] rej = new double[]{4, 7, 9};
This is quite terrible code, anyway to make it work you should have "rej" declared as static.
EDIT: As already suggested, you should also initalize it properly :
static {
rej[0] = 4;
rej[1] = 7;
rej[2] = 9;
}

Why does this Java program work counter to my expectations?

Why does this program work counter to my expectations?
I expect: 1 50 47 50
I get: 0 50 0 50
class Poppet {
int i;
Poppet(int i) {
i = i;
}
}
class Ideone
{
private final int i = 50; // Initialized final
private final int j; // Blank final
private final Poppet p; // Blank final reference
// Blank finals MUST be initialized in the constructor:
public Ideone() {
j = 1; // Initialize blank final
p = new Poppet(1); // Initialize blank final reference
System.out.println(p.i);
}
public Ideone(int x) {
j = x; // Initialize blank final
p = new Poppet(x); // Initialize blank final reference
System.out.println(p.i);
}
public static void main (String[] args)
{
Ideone t = new Ideone();
System.out.println(t.i);
Ideone r = new Ideone(47);
System.out.println(r.i);
}
}
Can you please explain why?
Change:
Poppet(int i) {
i = i;
}
to:
Poppet(int i) {
this.i = i;
}
by doing i = i you're assigning the method argument to itself while what you really want to do is to assign it to the class member variable.
Local variable shadowing, as described in JLS 6.4.1.
If you want to set the instance variable i, you have to prefix it as in this.i.
class Poppet {
int i;
Poppet(int i) {
this.i = i;
}
}

How to fill my array from another class?(Java)

This is my program that makes some calculations to a numbers into the array named "initialMarks". However I would like to fill the initialMarks array from another class using scanner.Could you help me to figure it out how to do that? Is it possible to outprint the result array "result" in a third class?
public class testingN
{
public static void main(String[] args)
{
int [] initialMarks = new int [4];
int [] result = new int [6];
result[0] = computedMarks(initialMarks[0], initialMarks[1])[0];
result[1] = computedMarks(initialMarks[2], initialMarks[3])[1];
for(int i=0; i< result.length; i++)
System.out.println(result[i]);
}
public static int [] computedMarks(int mark1, int mark2)
{
int [] i= new int [6];
for (int j = 0; j < i.length; j++)
{
if ((mark1 < 35 && mark2 > 35) || (mark1 > 35 && mark2 < 35))
{
i[j] = 35;
}
else
{
i[j] = (mark1 * mark2);
}
}
return i;
}
}
Your other class can have a method that returns a stream, and you can feed that into your Scanner's constructor.
In your other class's String getInitialMarks() method:
// Generate a string with all the "marks" as "stringWithMarks", separated by "\n" characters
InputStream is = new ByteArrayInputStream( stringWithMarks.getBytes( "UTF-8" ) );
return is;
Then in your first class, otherClass:
Scanner scanner = new Scanner(otherClass.getInitialMarks());
And proceed to read in the marks as if it were user input.
public class testingN
{
static int [] result = new int [6];
static int [] initialMarks = new int [4];
public static void main(String[] args)
{
Declaring result as class member (global variable) and being static should help your cause
Example: using from another class
public class AnotherClass {
public static void main(String[] args) {
// example
testingN.result[0] = 4;
System.out.println(testingN.result[0]);
}
}
Edit: Run the code below Here. You'll see it works just fine.
class testingN
{
static int [] result = new int [6];
static int [] initialMarks = new int [4];
}
public class AnotherClass {
public static void main(String[] args) {
// example
testingN.result[0] = 4;
System.out.println(testingN.result[0]);
}
}

Categories