Logcat only appears inside the onCreate method of MainActivity. I'm trying to use the Log (TAG, "msg"); within a class, and no log appears. why? in MainActivity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("Test", "test");
}
It works!
But this class doesn't work:
public class Imgpixel {
private static final String TAG = "Quicknotes";
public Imgpixel() {
}
String src="C:\\path_of_image\\img.jpg";
Mat imgRead = Imgcodecs.imread(src, IMREAD_COLOR);
int lin = imgRead.rows(); //get the number of rows
int col = imgRead.cols(); //get the number of cols
List<double[]> pixels=new ArrayList<>();//arraylist to save array rgb below
public void cor() {
for (int i = 0; i < lin; i++) {
for (int j = 0; j < col; j++) {
double [] rgb = imgRead.get(i, j);
pixels.add(0, rgb);
}
}
}
}
I am surprised your code example would compile. In your Imgpixel class the method cor() is defined outside the class, the variable pixels wouldn't be accessible and you have an extra }.
Call your cor method from another class (eg. from inside MainActivity) like this:
Imgpixel imgPixel = new Imgpixel();
imgPixel.cor();
When your Imgpixel class looks like this:
public class Imgpixel{
private static final String TAG = "Quicknotes";
String src="C:\\path_of_image\\img.jpg";
Mat imgRead = Imgcodecs.imread(src, IMREAD_COLOR);
int lin = imgRead.rows(); //get the number of rows
int col = imgRead.cols(); //get the number of cols
List<double[]> pixels=new ArrayList<>();
public Imgpixel(){
// initializing
}
public void cor() {
Log.v(TAG, "test"); //Remove the quotation marks from TAG in order to get value from the variable you set earlier
for (int i = 0; i < lin; i++) {
for (int j = 0; j < col; j++) {
double [] rgb = imgRead.get(i, j);
pixels.add(0, rgb);
}
}
}
} //this marks the end of the Imgpixel class
But you can also make Imgpixel a static class.
Related
I was solving past exams for my java class and I'm struggling with one of them. I keep getting wrong result and I think its because all of classes and instance variables are static. How do I avoid making them static? Also this question basically wants you to find same letters of the location given in args[1] and convert them to the "S" if they are near of the given location (Args are "K,K,K,Y-K,Y,M,M-K,Y,Y,Y 2,1 S" if you need)
public class MatrixRefill {
public static String[][] matrix;
public static int rows;
public static int cols;
public static String enemy;
public static String target;
public static void main(String[] args) {
target = args[2];
rows = Integer.parseInt(args[1].substring(0,1));
cols = Integer.parseInt(args[1].substring(2));
matrix = matrixCreator(args[0]);
enemy = matrix[rows][cols];
recursive(rows, cols, target);
printer(matrix);
}
public static String[][] matrixCreator(String mx) {
int ro = 0;
int co = 0;
for (int i = 0; i < mx.length(); i++) {
if (mx.substring(i,i+1).equals(","))
co++;
if (mx.substring(i,i+1).equals("-"))
ro++;
}
String[][] matriks = new String[ro+1][co/3+1];
ro = 0;
co = 0;
for (int j = 0; j < mx.length(); j++) {
if (mx.substring(j,j+1).equals(","))
co++;
else if (mx.substring(j,j+1).equals("-")) {
ro++;
co = 0;
}
else
matriks[ro][co] = mx.substring(j,j+1);
}
return matriks;
}
public static void recursive(int row, int col, String target) {
if (valid(row,col)) {
recursive(row+1,col, target);
recursive(row,col+1, target);
recursive(row,col-1, target);
recursive(row-1,col, target);
matrix[row][col] = target;
}
}
public static boolean valid(int row, int col) {
boolean result = false;
if (row >= 0 && row < matrix.length && col >= 0 && col < matrix[row].length)
if (matrix[row][col] == enemy)
result = true;
return result;
}
public static void printer(String[][] owo) {
for(int i = 0; i < owo.length; i++) {
for(int j = 0; j < owo[i].length; j++) {
System.out.print(owo[i][j]);
if(j < owo[i].length - 1)
System.out.print(" ");
}
System.out.println();
}
}
}
Remove the static keyword from your methods and instance fields. But to call them from within main you need to create an instance of the containing class (in this case the one that contains the main method) and use that to call the other methods. What I do sometimes is to create an instance method (i.e. non-static) and call that to start the process. Then everything that would be in main I would put in that method. Here is an example.
public static void main(String[] args) {
MatrixRefill mr = new MatrixRefill();
mr.start();
}
public void start() {
target = args[2];
rows = Integer.parseInt(args[1].substring(0,1));
cols = Integer.parseInt(args[1].substring(2));
matrix = matrixCreator(args[0]);
enemy = matrix[rows][cols];
recursive(rows, cols, target);
printer(matrix);
}
// rest of code here
}
By putting what was in main in the start method you can call the other instance methods and access instance fields without qualifying them with a reference to the class (i.e. in this case prefixing with mr.)
I have class Book with three fields - name, author and isbn
I`m trying to insert the fields in ArrayList and to print:
book1, author1, isbn
book2, author2, isbn2
and... to 10
code:
public class InsertBooks {
private static ArrayList<String> booksNames = new ArrayList<>();
private static ArrayList<String> booksAuthors = new ArrayList<>();
private static ArrayList<Integer> booksIsbn = new ArrayList<>();
private static ArrayList<Book> books = new ArrayList<>();
// adding books in ArrayList booksNames
private static void addBooksNames() {
for (int i = 0; i < 10; i++) {
booksNames.add("Book" + i);
}
}
// adding author in ArrayList booksAuthors
private static void addBooksAuthor() {
for (int i = 0; i < 10; i++) {
booksAuthors.add("Author" + i);
}
}
// adding author in ArrayList booksAuthors
private static void addBooksIsbn() {
for (int i = 0; i < 10; i++) {
booksIsbn.add(Integer.valueOf("isbn" + i));
}
}
public static void fillArrayListOfBooks() {
for (int i = 0; i < 10; i++) {
books.add(new Book((addBooksNames(), addBooksAuthor(), addBooksIsbn()));
}
}
}
You want to call all your add* functions first. Then in the loop of fillArrayListOfBooks() use those values.
void fillArrayListOfBooks()
{
addBooksNames();
addBooksAuthor();
addBooksIsbn();
for (int i = 0; i < 10; i++) {
dbooks.add(new Book(booksNames.get(i), booksAuthors.get(i), booksIsbn.get(i)));
}
}
You could easily get rid of those lists (unless you need them later):
void fillArrayListOfBooks()
{
for (int i = 0; i < 10; i++) {
dbooks.add(new Book("Book" + i, "Author" + i, "isbn" + i));
}
}
I'm wornig sqllite.i have some tables and i select name for example Customer Table and Price from AnotherTable. and i received two array list .first name's array list and secods price's array list.
this is a my source
private ArrayList<GetDictioanyClassByPosition> GetPKFromTable() {
price_array.clear();
ArrayList<GetDictioanyClassByPosition> my_list = d_Helper
.getAllPriceByPosition(0);
for (int i = 0; i < my_list.size(); i++) {
String AllPrice = my_list.get(i).getDictionaryPrice();
GetDictioanyClassByPosition cnt = new GetDictioanyClassByPosition();
System.err.println(AllPrice + "AllPrice");
cnt.setDictionaryPrice(AllPrice);
price_array.add(cnt);
}
return price_array;
}
this is a method to check price's array list and this method check name's array list
public void AllInfoFromLoanAnalysis() {
name_list.clear();
ArrayList<GetAllDictionariesClass> contact_array_from_db = d_Helper
.getAllInformationFromLoanAnalysis_Table(1, 1);
Log.e("Sizee", contact_array_from_db.size() + "sizee");
for (int i = 0; i < contact_array_from_db.size(); i++) {
int DictionaryID = contact_array_from_db.get(i).getDictionaryID();
System.out.println(DictionaryID + "DictionaryID");
GetDictioanyClassByPosition object = new GetDictioanyClassByPosition();
String name = null ;
ArrayList<GetDictioanyClassByPosition> DictionaryIdList = d_Helper
.GetDictionaryIdList(DictionaryID);
System.out.println(DictionaryIdList.size() + "DictionaryIdList");
Log.e("Sizee2", DictionaryIdList.size() + "sizee2");
for (int j = 0; j < DictionaryIdList.size(); j++) {
name= DictionaryIdList.get(j).getDictionaryName();
Log.e("object", name + "object");
object.setDictionaryName(name);
name_list.add(object);
}
for (int j = 0; j < price_array.size(); j++) {
String AllPrice = price_array.get(i).getDictionaryPrice();
object.setDictionaryPrice(AllPrice);
object.setDictionaryName(name);
price_array.add(object);
}
agroproductslistview.setAdapter(agroproduct_adapter);
}
}
and i called my BaseAdapter Like this
_adapter = new LoanProductAdapter(
getApplicationContext(), R.layout.productlistadapter,
name_list);
public class GetDictioanyClassByPosition {
private String DictionaryName;
private String DictionaryPrice;
public String getDictionaryName() {
return DictionaryName;
}
public void setDictionaryName(String DictionaryName) {
this.DictionaryName = DictionaryName;
}
public String getDictionaryPrice() {
return DictionaryPrice;
}
public void setDictionaryPrice(String DictionaryPrice) {
this.DictionaryPrice = DictionaryPrice;
}
}
i can selected and show my prices and names in different array list but i want to marge both array list and would adapter in my list view
.how i can solve my problem?
if anyone knows solution please help me
thanks
Please refer below example.
public ArrayList<customObject> _historyArrayList = new ArrayList<customObject>();
public ArrayList<customObject> _completedArraylist = new ArrayList<customObject>();
For merging simply use:
_historyArrayList.addAll(_completedArraylist);
Note: Make sure your customObject are same
My apologize, I have a class on my Project, called test01.java. And i used the library from Tadaki Graphlib contained many class. On of them is Graph.java.
Test01.java:
public class test01 extends Graph{
public test01(String name, int n) {
super(name);
public test01(String name, int n) {
super(name);
graphLib.Vertex vList[] = new graphLib.Vertex[n];
for (int i = 0; i < n; i++) {
vList[i] = new graphLib.Vertex(String.valueOf(i));
addVertex(vList[i]);
}
int deg = 0;
System.out.println("<---------- Random val ---------->");
addArc(vList[0], vList[1], String.valueOf(0)); deg++;
addArc(vList[1], vList[0], String.valueOf(1)); deg++;
System.out.println("Vertex-0 with Vertex-1");
System.out.println("Vertex-1 with Vertex-0");
int k = 2;
int l;
int m=0;
Random randomval = new Random();
int isAvailInt [] = new int[n];
while (k<n) {
for(l=0;l<k;l++){
isAvailInt [l]= Integer.parseInt(vList[l].toString());
m=isAvailInt[l];
}
int chosen = randomval.nextInt(m);
addArc(vList[k], vList[chosen], String.valueOf(k));
System.out.println("Vertex-"+k+" with Vertex-"+chosen+
" exp = " + String.valueOf(k));
k++;
}
}public static void main(String args[]) {
int n;
String num = JOptionPane.showInputDialog("Masukkan nilai jumlah iterasi = ");
String degnum = null;
n = Integer.parseInt(num);
int deg []= new int [n];
test01 t = new test01("test",n);
System.out.println("<---------- Vertex-i = Degree-i ------------>");
graphLib.Graph g= new Graph("test");
int [][]adj = g.getAdjacent();
System.out.println(adj[0][0]);
for (int i=0; i<t.getSize();i++){
for (int j=0; j<t.getSize();j++){
}
}}
and one other class called Graph.java
public class Graph extends GraphBase { int adjacent[][] = null;
public Graph(String name) {
this.name = name;
vertexes = Utils.createVertexList();
arcs = Utils.createArcList();
a2vHead = new HashMap<>();
a2vTail = new HashMap<>();
v2a = new HashMap<>();
}
public int[][] getAdjacent() {
int n = vertexes.size();
adjacent = new int[n][];
for (int i = 0; i < n; i++) {
adjacent[i] = new int[n];
for (int j = 0; j < n; j++) {
adjacent[i][j] = 0;
}
}
if (directed) {
for (int i = 0; i < n; i++) {
Vertex v = vertexes.get(i);
for (Arc a : v2a.get(v)) {
Vertex t = a2vTail.get(a);
int l = vertexes.indexOf(t);
adjacent[l][i]++;
}
}
} else {
for (int i = 0; i < n; i++) {
Vertex v = vertexes.get(i);
for (Arc a : v2a.get(v)) {
Vertex t = a2vTail.get(a);
if (!t.equals(v)) {
int l = vertexes.indexOf(t);
adjacent[i][l]++;
adjacent[l][i]++;
}
}
}
}
checkConnectedness();
return adjacent;
}}
From above, method - int [][] Adjacent() - has an array return value:
return adjacent;
Then I want to received it with array variable declared:
int [][]adj = g.getAdjacent();
But when I run the program, the code :
System.out.println(adj[0][0]);
Has appeared error :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
I've declare the variable vertexes in Graph.java that extended from other class, GraphBase.java:
vertexes = Utils.createVertexList();
How do I obtain an array value form a variable adjacent in Graph.java to test01.java and how do I display it with System.out.println() ?
Well you haven't shown where vertexes is initialized (or even declared) in Graph. I suspect it's empty, so when you execute this code:
public int[][] getAdjacent() {
int n = vertexes.size();
adjacent = new int[n][];
...
return adjacent;
}
... you'll end up with an empty array. That would cause the problem you've seen. You can easily check the size in your main method:
System.out.println(adj.length);
I suspect you'll find it's 0. Either that, or adj[0].length is 0.
It's not clear how you expect the Graph to find any vertexes - you don't supply it with any, or even the value of n. You just call the constructor with a string:
graphLib.Graph g= new Graph("test");
Unless that's meant to be the name of a file which is loaded in the constructor, there's nowhere for it to get data from. You need to take a step back and think about where you expect the data to come from, then make sure that it can actually flow through your program. The problem isn't getting the array reference back to main - the problem is that the array is empty.
I doubted about this line returning 0.
int n = vertexes.size();
You can reproduce this issue by running below code
int adjacent[][] = new int[0][];
System.out.println(adjacent[0][0]);
You will get the same exception
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
To solve this issue
Make sure before proceeding vertexes have expected values.
I'm hoping some of you are familiar with the Processing development environment for Java. I've searched the site, but wasn't able to find too much. So I'm supposed to be passing command-line arguments from the 'args' string array from
public static void main(String[] args )
...to arrays in a setup() function so that I can parse them and use them as values for the heights of bar graphs, which are to be drawn on a graphical window. I don't understand why I keep getting a NullPointerException error at line 16...
for(int i = 0; i < tempArgs.length; i++) {
dataArray[i] = Integer.parseInt(tempArgs[i]);
}
.....as I thought the tempArray array should have values in it, which seems to be bourne out by the System.out.println statement I used to test this at the end of the main method. Here's the whole code, it's quite short. Any help would be much appreciated.
public class Main extends PApplet {
int[] dataArray ;
int[] normalizedData;
String[] tempArgs;
#Override
public void setup() {
/*size(dataArray[2], dataArray[3]);
smooth();
background(255);*/
for(int i = 0; i < tempArgs.length; i++) {
dataArray[i] = Integer.parseInt(tempArgs[i]);
}
int max = dataArray[0];
for(int i = 0; i < dataArray.length; i++) {
if(dataArray[i] > max) {
max = dataArray[i];
}
}
for(int i = 0; i < dataArray.length; i++) {
normalizedData[i] = (dataArray[i] / max) * (height - 20);
}
for(int i = 0; i < normalizedData.length; i++) {
fill(255,34,65);
rect(3, 5, 10, normalizedData[i]);
}
size(dataArray[2], dataArray[3]);
smooth();
background(255);
}
public static void main(String[] args) {
String[] tempArgs = new String[args.length + 2];
tempArgs[0] = "--bgcolor=#FFFFFF";
tempArgs[1] = "project1.Main";
for(int i = 2; i < tempArgs.length; i++) {
tempArgs[i] = args[i-2];
}
System.out.println(tempArgs.length);
PApplet.main(tempArgs);
}
}
You are missing the creation of dataArray.
#Override
public void setup() {
// add this line
dataArray = new int[tempArgs.length];
for(int i = 0; i < tempArgs.length; i++) {
dataArray[i] = Integer.parseInt(tempArgs[i]);
}
...
}
Also, the tempArgs should be static, because in main() there is no instance of Main class yet, so only saving tempArgs will allow to use it later in setup().
public class Main extends PApplet {
int[] dataArray ;
int[] normalizedData;
static String[] tempArgs;
...
public static void main(String[] args) {
tempArgs = new String[args.length + 2]; // note change in this line!
...
}
}
Possibly it could be done simplier, because I see you are passing tempArgs to PApplet.main(), however I do not know how this applet framework works.
you have 2 separate tempArgs arrays.
One is created locally in main(), and that one prints out correctly
the other one belongs to the class and is never instantiated.