I'm learning the AP CSA course and tried to print these arrays. It keeps saying "can't find symbol" and not gonna lie I have absolutely no idea what that means.
//ArrayList:
public class al{
public static void main(String[] args){
List l = new ArrayList();
System.out.print(l.size());
}
}
//2DArray:
public class arrays{
public static void main(String[] args){
int[][] arry = {{1,2,3},
{1,2,3},
{1,2,3}};
printArray(arry);
}
}
In your first program to print the ArrayList, it is failing because you need to import the List and ArrayList in your program, please try this
import java.util.ArrayList;
import java.util.List;
public class al{
public static void main(String[] args){
List l = new ArrayList();
System.out.print(l.size());
}
}
In the second program the printArray method you called is missing in definition, i.e you have to define the printArray method in your class, you are using it from method main then you need to declare it static like below
static void printArray(int[][] arr){
for(int i=0; i<arr.length; i++){
String row = "";
for(int j=0; j<arr[0].length;j++){
row+=arr[i][j]+" ";
}
System.out.println(row);
}
}
Also while you are learning, please also read about the naming conventions in java (i suppose you are learning java), like a Class name should start from a capital letter.
Related
Why I am getting the error while runtime:
Exception in thread "main" java.lang.NullPointerException at arrayTest.main(arrayTest.java:5)
Source code is as following:
public class arrayTest{
int b;
public static void main(String[] args){
arrayTest[] a= new arrayTest[2];
a[0].b=10;
System.out.println(a[0].b);
}
}
Thanks
Sunil
You haven't added any arrayTest instances to your array. You've only initialized the array, so arrayTest[0] is null. To add objects to the array:
arrayTest[0] = new arrayTest();
arrayTest[1] = new arrayTest();
When doing Object-Oriented-Programming (OOP) you need to think a bit differently.
Here what you are doing is creating a new object arrayTest from the class arrayTest.
This object need a constructor and fields which are properties so it can be well defined for instance: size, age or eyes colour for a person.
Here what do you want your object to be?
For instance, I want to do an ArrayList of n object of the same type :
First, my field is going to be an ArrayList because that's what I want to create.
I have the following class :
import java.lang.reflect.Type;
import java.util.ArrayList;
class arrayTestType {
private final ArrayList arrayTest;
}
What should my constructor return?
Well, an array of type Type with n elements is ArrayList<Type>(n)
so my constructor should be written like this :
public arrayTestType(Type type, int length) {
arrayTest = new ArrayList<Type>(length);
}
So finally our class is :
import java.lang.reflect.Type;
import java.util.ArrayList;
class arrayTestType {
private final ArrayList arrayTest;
public arrayTestType(Type type, int length){
arrayTest = new ArrayList<Type>(length);
}
}
Now when we are going to call :
arrayTestType stringArrayOfLength5 = new arrayTestType<String>(5);
We are going to get an object well defined.
Now you need to add things in order for it not to be null hence the null pointer !
Good luck !
I have a char array: my_array={"a","b","c"}
And I need to get 1 specific element and put it as function parameter
For example: function myFunction({"b"}){}
I tried this but got an error:
unexpected character
I learned from TTCN-3 and I try this on it.
This will help you. Use this kind of code will help you to pass one parameter each time.
public class MyClass {
public static void main(String args[]) {
MyClass mc = new MyClass();
char my_array[]={'a','b','c'};
char returnValue;
for(int i=0;i<my_array.length;i++){
returnValue = mc.charReturn(my_array[i]);
System.out.println("Got it from method:"+returnValue);
}
}
public char charReturn(char fromClass){
return fromClass;
}
}
I am new in hadoop and mapreduce programming and don't know what should i do. I want to define an array of int in hadoop partitioner. i want to feel in this array in main function and use its content in partitioner. I have tried to use IntWritable and array of it but none of them didn't work . I tried to use IntArrayWritable but again it didn't work. I will be pleased if some one help me. Thank you so much
public static IntWritable h = new IntWritable[1];
public static void main(String[] args) throws Exception {
h[0] = new IntWritable(1);
}
public static class CaderPartitioner extends Partitioner <Text,IntWritable> {
#Override
public int getPartition(Text key, IntWritable value, int numReduceTasks) {
return h[0].get();
}
}
if you have limited number of values, you can do in the below way.
set the values on the configuration object like below in main method.
Configuration conf = new Configuration();
conf.setInt("key1", value1);
conf.setInt("key2", value2);
Then implement the Configurable interface for your Partitioner class and get the configuration object, then key/values from it inside your Partitioner
public class testPartitioner extends Partitioner<Text, IntWritable> implements Configurable{
Configuration config = null;
#Override
public int getPartition(Text arg0, IntWritable arg1, int arg2) {
//get your values based on the keys in the partitioner
int value = getConf().getInt("key");
//do stuff on value
return 0;
}
#Override
public Configuration getConf() {
// TODO Auto-generated method stub
return this.config;
}
#Override
public void setConf(Configuration configuration) {
this.config = configuration;
}
}
supporting link
https://cornercases.wordpress.com/2011/05/06/an-example-configurable-partitioner/
note if you have huge number of values in a file then better to find a way to get cache files from job object in Partitioner
Here's a refactored version of the partitioner. The main changes are:
Removed the main() which isnt needed, initialization should be done in the constructor
Removed static from the class and member variables
public class CaderPartitioner extends Partitioner<Text,IntWritable> {
private IntWritable[] h;
public CaderPartitioner() {
h = new IntWritable[1];
h[0] = new IntWritable(1);
}
#Override
public int getPartition(Text key, IntWritable value, int numReduceTasks) {
return h[0].get();
}
}
Notes:
h doesn't need to be a Writable, unless you have additional logic not included in the question.
It isn't clear what the h[] is for, are you going to configure it? In which case the partitioner will probably need to implement Configurable so you can use a Configurable object to set the array up in some way.
Just wondering, is it not allowed to use Starts/EndsWith functions with Scanner? I'm trying to make a simple program where some elements from the set of array that I will provide will be printed out using those functions. But obviously there is an error that I don't know where it is. Please help
import java.util.Scanner;
public class test {
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
String[] animals = new String[5];
System.out.println("Enter animal type");
for (String j : animals){
if (j.startsWith("do"))
System.out.println(j);
}
}
}
I'm writing a game using Surfaceview and have a question relating to saving Data into a Bundle.
Initially, I had an arraylist which stored the Y co-ordinates (in the form of Integers) of sprites that will move only up and down. Declared as:
static ArrayList<Integer> ycoordinates = new ArrayList<Integer>();
I saved them to a Bundle using the following:
myBundle.putIntegerArrayList("myycoordinates", ycoordinates);
And restored them using this:
ycoordinates.addAll(savedState.getIntegerArrayList("ycoordinates"));
This all worked perfectly. However, I've had to change the whole coordinates system so it's based on Delta time to allow my sprites to move at a uniform speed across different screens. This is, again, working perfectly.
However, as a result of this change, I now have to store these values as floats rather than integers.
So, I am declaring as:
static ArrayList<Float> ycoordinates = new ArrayList<Float>();
So that's the background, now my question is, how do I store and restore values from a Float Arraylist? There doesn't seem to be a "putFloatArrayList" or "getFloatArrayList".
(I've used an Arraylist rather than an Array as the number of sprites needs to be dynamic).
Any help would be appreciated.
Many thanks
I've written a couple of simple methods to convert between List and float[]. You can use the Bundle putFloatArray() and getFloatArray on the float[].
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args){
List<Float> in = new ArrayList<Float>();
in.add(3.0f);
in.add(1f);
in.add((float)Math.PI);
List<Float>out = toList(toArray(in));
System.out.println(out);
}
public static float[] toArray(List<Float> in){
float[] result = new float[in.size()];
for(int i=0; i<result.length; i++){
result[i] = in.get(i);
}
return result;
}
public static List<Float> toList(float[] in){
List<Float> result = new ArrayList<Float>(in.length);
for(float f : in){
result.add(f);
}
return result;
}
}