how to declare array pointer first, and initialize later in java - java

When I want to use an array for storing the inputs from Scanner,
but don't know how many tokens it contain, aren't there any ways to get all the inputs stored in the array with the exact same size as the tokens?
The situation is like this.
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int[] arr; // just declare not initialize
int i = 0;
while(sc.hasNextInt()){
arr[i++] = sc.nextInt(); // of course it would cause an error,
// but as I don't know how many tokens it has,
// I can't initialize like
// int[] arr = new int[number of tokens]
}
}
In this situation, I declared some array pointer first, arr, but didn't know the size of the tokens so I couldn't initialize it. Instead I was looking for the ways - making the pointer first, then storing all the data, and then the original pointer pointing the arrays of inputs-stored-array.
Wouldn't there be any ways for this?

When you are not sure of the size of array you need you can use java.util.ArrayList instead of array. ArrayList internally consists of an array and a logic for resizing it based on need. Please refer the below code for your reference :
import java.util.*;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
List<Integer> arrList = new ArrayList<>(); // No need to specify size
int i = 0;
while(sc.hasNextInt()){
arrList.add(sc.nextInt()); // it will take care of resizing the array internally based on the inputs
}
}

Related

How to enter one dimensional array filled with unknown values of variable

I'm a Java beginner and I don't understand how to make it. When I write in my code something like in the example, my IDE underlines it and says it's wrong when I only started writing my code. Can anybody help me guys?
Example:
public class ArrayUtils {
public static int[] lookFor(int[] array) {
int[] array = {};
}
}
The variable named array is already passed in as a parameter. Which means that you cannot create a new int[] named array inside the java method. Try naming it something else.
Syntax with {} means initialization of your array like int[] array = {1,2,3}.
But you can't initialize the variable with the same name as parameter's name.
You can assign a new array to the variable:
public static int[] lookFor(int[] array) {
array = new int[6]; // assign to variable new array with length 6
array = new int[]{1,3,5}; // assign to variable new array with initialized values
}
Note: in first case all 6 values will be zero
Update: as it was mentioned by #ernest_k reassigning method parameters is a bad practice. To avoid it method parameter usually marked as final int[] lookFor(final int[] array)

Java, fill an array with a supplied number

import java.util.*;
public class IncreasingSum
{
public static void ArrayList(int[] args)
{
Scanner kb=new Scanner(System.in);
System.out.print("Enter a number:");
int num=kb.nextInt();
{ for (int loop=0; loop<num; loop++)
ArrayList <X> myList = new ArrayList<X>();
myList.add(loop);
}
}
}
I have imported the java.util.*; but it keeps giving error on ArrayList.
There are a number of issues
should be static void main(String [] args), instead of static void ArrayList(int [] args)
X should be Integer
Loop should be around the add(), not the new ArrayList()
extra braces not required outside the loop
Possible flaw
Code should should probably use add(n) rather than add(loop) to "fill" the array with the same number
If above true then just use Collections.fill(myList, num); and get rid of your loop
Fixed
import java.util.ArrayList;
import java.util.Scanner;
public class IncreasingSum {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.print("Enter a number:");
int num = kb.nextInt();
ArrayList<Integer> myList = new ArrayList<Integer>();
for (int loop = 0; loop < num; loop++) {
myList.add(loop);
}
}
}
Syntax Errors
The reason you get an error on the ArrayList line is because of the way you've instantiated it.
Here's an example of how to use ArrayLists with generics:
http://docs.oracle.com/javase/tutorial/java/generics/why.html
Basically in your declaration, you'll need to substitute X with the class of elements you want to store in the ArrayList. In your case that would be Integer.
ArrayList <X> myList = new ArrayList<X>(); //Throws error
ArrayList<Integer> myList=new ArrayList<Integer>(); //Will suit your need
Once you've fixed the list declaration, you'll also need to fix the line with the for loop. The starting braces of your for loop should come after the loop declaration to fix your other syntax error.
{ for (int loop=0; loop<num; loop++) //Throws error for your program
for (int loop=0; loop<num; loop++){ //The declaration you need
Other potential errors:
You'll also need to include the main method in the class to execute your program.
FYI:
Java convention for method names is starting with a lowercase letter. So even if you did have a method called ArrayList, arrayList would be more in line with the method naming convention.
https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
Use specific class called Integer for generics as you are adding Integer within array list before for loop and add element within loop:
ArrayList <Integer> myList = new ArrayList<Integer>();
You need brackets after for loop. Right now the only intruction in for loop is to create new ArrayList several times. Array list should be declared above for loop.

How to allocate a null array within a function in Java?

Trying to get a function to fill multiple arrays with values. Would love to be able to do this:
public class Demo {
public static void getData(int[] array1, int[] array2) {
array1 = new int[5];
array2 = new int[5];
}
public static void main(String[] args) {
int[] array1 = null;
int[] array2 = null;
getData(array1, array2);
System.out.println(array1[0]); // line 11
}
}
Exception in thread "main" java.lang.NullPointerException
at Demo.main(Demo.java:11)
This gives a NullPointerException, but I'm not exactly sure why. I can't allocate the arrays in main() because there's no way to know in advance how large they'll be. Could use a List, or a 2D array, but is there a way to do this without either?
As several other commentators have mentioned, the problem here is misunderstanding the notion of pass-by-reference. Without getting into the nitty-gritty, as a rule of thumb any time you say a = ... where a is an Object (which includes arrays) you are disconnecting the name a from a previous value, and pointing it instead at your new value. There is no connection between the two, and no way to reference past values of a. So in your case, by setting array1 and array2 to new values you are no longer referencing the objects passed into the function. In this case the function arguments are null, not actual objects, but that doesn't change anything here.
The more important question however is what to do about this? Obviously the current design is unworkable, but how exactly can we do this? It's a common problem, so unsurprisingly there are many viable options.
Use static variables
The easiest thing you can do here is simply ensure both main() and getData() are working with the same variable, by using static class variables both methods can reference. While easy for simple projects, this breaks down quickly and starts introducing more problems than it solves as your project's size and scope gets bigger. Use with caution.
public class Demo {
public static void getData(int[] array1, int[] array2) {
array1 = new int[5];
array2 = new int[5];
}
public static void main(String[] args) {
int[] array1 = null;
int[] array2 = null;
getData(array1, array2);
System.out.println(array1[0]); // line 11
}
}
Use a dedicated object, and a pass around references to that object
Alternatively, we can avoid static variables by constructing a single holder object and passing around references to it. We can then freely change the instance variables inside our holder.
public class Holder {
// Should really be private, with constructors/getters/setters
// but for brevity we'll access them directly here. Don't
// take this shortcut in production code
public int[] array1;
public int[] array2;
}
public class Demo {
public static void getData(Holder holder) {
holder.array1 = new int[5];
holder.array2 = new int[5];
}
public static void main(String[] args) {
Holder holder = new Holder();
getData(holder);
System.out.println(holder.array1.length+" "+holder.array2.length);
}
}
Use a resizable data structure
You mention your concern is you don't know ahead of time the size of the loaded data, which makes this a perfect candidate for a List or other data structure. You also mention you don't want to do this, which is fine, but be really sure you actually need arrays - they provide very few benefits over proper collection types, and many more hassles. Using a resizable data structure lets us construct and populate the same object separately.
import java.util.*;
public class Demo {
public static void getData(List<Integer> ls1, List<Integer> ls2) {
// bad formatting for brevity, don't do this either
ls1.add(1); ls1.add(2); ls1.add(3); ls1.add(4); ls1.add(5);
ls2.add(1); ls2.add(2); ls2.add(3); ls2.add(4); ls2.add(5);
}
public static void main(String[] args) {
List<Integer> ls1 = new ArrayList<>();
List<Integer> ls2 = new ArrayList<>();
getData(ls1, ls2);
System.out.println(ls1.size()+" "+ls2.size());
}
}
Compartmentalize this behavior entirely into a proper object
All of the above are reasonable options given specific use cases, but they all pale in comparison to the robustness, power, and code-safety of creating a proper object and compartmentalizng the behavior entirely. What do I mean by this? Instead of having a getData() method which applies some business logic to some existing variables, group the variables and the logic together into an object - this is the core tenant of Object Oriented Programming.
public class Data {
// Again, getters/setters, but at least these are final, that's an improvement
public final int[] array1;
public final int[] array2;
public Data() {
array1 = new int[5];
array2 = new int[5];
}
}
public class Demo {
public static void main(String[] args) {
Data data = new Data();
System.out.println(data.array1.length+" "+data.array2.length);
}
}
By using a dedicated class, we hide the logic of building this data from the caller. Instead, the main() method simply constructs a Data() object, and can trust simply by constructing the object that it now has all the data it needs. No need to worry about references, array sizes, or anything else; all those decisions are dealt with correctly internally and hidden away from the caller.
Java will always pass the reference to your array declared in main so not initializing it will always get an NPE. I think you should just use List for this since you need your method to be able to define the size. Just convert to array if you need the final output to be an array.
array1 = new int[5];
array2 = new int[5];//this will create variable in function body scope
that is the reason you are getting null pointer
try:
public static void main(String[] args) {
int[] array1 = new int[5];
int[] array2 = new int[5];
getData(array1, array2);
}
public static void getData(int[] array1, int[] array2) {
// fill array1 and array2 with data
}
the way you are trying to achieve it, is not a possible way in Java
You could make an array with length 0:
public void getData(int[] array1, int[] array2) {
array1 = new int[5];
array2 = new int[5];
}
public void main(String[] args) {
int[] array1 = new int[0];
int[] array2 = new int[0];
getData(array1, array2);
}
// fill array1 and array2 with data

Array of ArrayLists Null Pointer Exception

I'm getting a null pointer exception on the following code (part of a larger program - the exception is thrown on the line where "add" is called).
public class A
{
static ArrayList<Integer> sets[];
public static void main(String[] args)
{
sets = new ArrayList[5];
sets[0].add(1);
}
}
I also do not understand why the compiler is requiring me to make any class level variables static (e.g. the ArrayList). As far as I can tell, these things shouldn't be in a static context (in terms of coding practice, not compiler problems) and yet the compiler is requiring it.
Thanks in advance.
sets = new ArrayList[5];
Just fills 5 spots with null
You need to explicitly set ArrayList() for each position before doing add() call.
Example:
sets[0] = new ArrayList<Integer>();
sets[0].add(5);
The line
sets = new ArrayList[5];
allocates the array, but does not place an ArrayList in any element of the array.
You would need
sets[0] = new ArrayList<Integer>();
sets[0].add(1);
It's because your array is initialized with null values.
//it will initialize sets variable
sets = new ArrayList[5];
//but set[0], set[1]... and on are null
You should initialize the array items as well before using them
sets[0] = new ArrayList<Integer>();
sets[0].add(1);
Also, for a better design, you should program oriented to interfaces instead of the class. See What does it mean to "program to an interface"? for more info.
In short, your code should look like
public class A {
static List<Integer> sets[];
public static void main(String[] args) {
sets = new List[5];
sets[0] = new ArrayList<Integer>();
sets[0].add(1);
//extending your code in order to use more than one value of the List array
sets[1] = new ArrayList<Integer>();
sets[1].add(20);
for(List<Integer> list : sets) {
if (list != null) {
System.out.println(list.get(0));
}
}
}
}

Adding/Appending elements to an array - Java

I am building a method which takes as argument, an array of decimal numbers, and a decimal threshold. The method should output all numbers from the list that are greater than the threshold.
My plan is to execute a for loop and examine each number in the array, and if that number (i) is greater than the threshold (x), to append to my result list. My problem is that I'm unable to add/append to the result list.
I have System.out.println("Nothing here"); just to help me see if it's actually going through the for loop or not, but my IDE is saying that calling list.add(a[i]); is wrong. I am a beginning programmer and not sure on how to fix this. Here is my code:
public class a10 {
public static void main(String[] args) {
double a[] = {42, 956, 3,4};
threshold(a, 2);
}
public static void threshold(double[] a, double x){
double list[] = {};
for (double i:a){
if (i<22){
list.add(a[i]);
}else{
System.out.println("Nothing here");
}
}
}
Your list is actually an array (double[]), which is NOT an object with the method add. You should treat it as a regular array (which in your case between, you have initialized to be an empty array, which means you can't set any elements in it).
What you should do is use an actual implementation of Lis instead (e.g an ArrayList) and then you can actually use the add method:
List<Double> result = new ArrayList<Double>();
for (double i:a){
if (i>x){
list.add(a[i]);
}else{
System.out.println("Nothing here");
}
}
Notice also that you had the number '22' hard coded (you should use x)
There's no method add for arrays in Java. You should declare list as:
List<Double> list = new ArrayList<Double>(); //or some other type of list

Categories