|
it's work fine the output :
Array values after sorting:
10 10 10
so my question how java invoke First_Array(a); not Second_Arra(a) ,
|
|
|
|
|
I copied the code from your question and it definitely does not compile.
|
|
|
|
|
public class ArrayDemo
{
public static void main(String[] args){
int[] a = {99, 5, 4, };
Try_Array Obj =new Try_Array();
Obj.cool(a);
System.out.println("Array values after sorting:");
for (int i = 0; i < a.length; i++){
System.out.print(a[i] + " ");
}
System.out.println();
}
}
public class Try_Array
{
public void cool(int[] a)
{
Second_Arra(a);
First_Array(a);
}
private void First_Array(int[] a )
{
for (int i = 0; i < a.length; i++)
a[i] = 10;
}
private void Second_Arra(int[] a )
{
for (int i = 0; i < a.length; i++)
a[i] = 100;
}
}
Ok Sorry my Mistake , i fix it now , why and how java invoke First_Array(a); not Second_Arra(a); is that random , I got confuse
|
|
|
|
|
there is nothing random in this, it calls the methods in the order you have coded. So First_Array is called last, and all values are set to 10, there is no sorting done.
|
|
|
|
|
thanks so you saying , java did first calling for Second_Arra(a); then did call First_Array(a);
so Java brought ma final value to the main method .
ok that make sense now .
but still not logic for me how java process the code , for example :
I didn't create instance variable for the class Try_Array.
i don't declare variable like int a[]; and i don't create set() and get() . i don't even do return method !
how for loop (int i = 0; i < a.length; i++) on the main method knows that a[] is got handled with Try_Array class and that array got full with new value !
I just no longer able to trace how java process this I really lost and got crazy to fail understand simple thing
when I build the code I build return method that brought final value I usually build private instance variable for the class . but I got confuse when I saw this code on book not adhere to what I learn
|
|
|
|
|
The code does exactly what you tell it:
- You create the array
a with some values - You create the Obj object
- You call Obj.cool passing in the array
- Obj.cool calls Second_Arra which sets all the values to 100
- Obj.cool next calls First_Array which sets all the values to 10
- You print the contents of the array
|
|
|
|
|
ok thank you so much I would like to clear my point this time how about passing variable
public class ArrayDemo
{
public static void main(String[] args){
int a = 5;
Try_Array Obj =new Try_Array();
Obj.cool(a);
System.out.println("A value After Calling:");
System.out.print(a );
}
}
public class Try_Array
{
public void cool( int a)
{
First_Array(a);
Second_Arra(a);
}
private void First_Array(int a )
{
a=4;
}
private void Second_Arra(int a )
{
a=6;
}
}
the out put is :
A value After Calling:
5
it' should be 6 ; but seam java ignore invocation with variable but java not behave like this with array
do you see my point when I pass array it got effect and back with different but when I send variable back nothing and it will not get effect or change the variable !
|
|
|
|
|
No, it should be 5. Since this is a simple variable, it is passed by value, so each called method gets a copy of the variable a. When they try to change it they only change the copy they have received, not the original variable. You should go to The Java™ Tutorials[^], especially section Passing Information to a Method or a Constructor[^] which explains it in detail.
|
|
|
|
|
Dear Richard your comment help me a lot thank you for guide me to the important topic and basic point that I really miss , you know what I spend 3 month reading with java when I reach recursion it was setback for me I shocked when book give example with mergesort, he build array on Fantasy way that I got really confuse and frustrated that i'm able to follow so I just back for reading previous chapters .
thank you so much I really appreciate your time.
|
|
|
|
|
Hello!
This is my first topic and I will search for other forums as well that might be more into ANN's. As the subjects indicates I have a huge interest in ANN's.
I would like to find people that are intereseted in ANN's and want to code or is coding already in Java and learn more about code implementation, testing and experimenting with ANN's.
I have built my own "library" and implemented it partially. It solves the XOR problem by using LMS algorithm for updating weights and now I am trying to implement the Backpropagation algorithm. Which is a pain in the ass becuase of the way I am storing stuff in my arrays. Meaning that calculating the backpropagation-deltas for the hiddenlayer is a bit of a challenge right now and that is where I am.
I have also creted classes that are objects such as Layer.java, Neuron.java and Weight.java. I will rewrite my code so that I use these classes as Layer[], Neuron[] and Weight[] instead of double[] and int[].
My bachelor topic had to do with topology mutating ANN's that use a simple genetic mutation of the weights to update the network and also the topology; a kind of placticity.
I am up for sharing this on GitHub and further developing it together with someone that also has a huge interest in this stuff! My goal is to reach to the point where a reward-based ANN, combined with this new code structure I am working on and with the code from my bachelor project. For example I would like to try to make ANN's cooperate with other ANN's. So that one ANN acts as visual input while another ANN acts as a generalization network trying to understand and categorize what is "seen".
So is anyone interested in helping and discussing this and coming with ideas and challenges? Or do you have any thoughts about this?
Also if you know any forum that is more suited for ANN then please suggest it! I would be very happy!
|
|
|
|
|
Member 13241109 wrote: I have also creted classes that are objects such as Layer.java, Neuron.java and Weight.java. I will rewrite my code so that I use these classes as Layer[], Neuron[] and Weight[] instead of double[] and int[]. Why weights and neurons? This just adds a whole bunch of pointless overhead, and no significant encapsulation. The only thing worth encapsulating is a whole layer, and only because you can have different types of layer (standard, conv, pooling, etc).
|
|
|
|
|
Yes true you are right I don't really have any use of those objects right now. The code works and would work without these object classes. Hopefully they will prove to be useful when say I would want to implement plasticity or list all inputs and outputs from neuron X in the hidden layer Y. I don't know if there is a better way. Also I hope it will make the code easier to read and give me more control over what goes where. This also forces the objects to be of the type double[] or I can create additional methods that only apply to the objects of type Weights or Neuron and it would structure the code better. I'll see how it plays out.
|
|
|
|
|
Hi experts,
I hope someone can tell me how to write the code which I have stumbled :
<pre>subject m = new subject();
List<subject>subject = new ArrayList<subject>();
String[] subjects = subject.toArray(new String[subject.size()]);
subjects = request.getParameterValues("subject");
for (int i = 0; i< subjects.length; i++){
if (subjects != null) {
m.setSubjects(subject);
mgr.insertSubject(m);
Basically, I do not know how do I add the subjects into generic List<subject>subject.
Hope someone can guide me here.
Tks.
|
|
|
|
|
There are several confusions in your code, you are creating List, then going back to static arrays, then... Wait right,
What you need to do is, you need to add subjects, then convert it to array, I would suggest performing the conversion from List to array at the end. What happens is that your list is employ, and thus your array is also of zero size, so you cannot add anything and if you do add something to list (later on, after creating the array), you do not get that in the array.
Besides, Java doesn't allow same name to be reused for multiple types (your class type, variable name, etc.) so please review the code, and if someone gave it to you, lure them with you to the Java tutorials or classes.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
So you've got an array of strings, and you want to convert them to instances of your custom subject class?
Since you haven't shown us what the strings look like, or what your subject class looks like, there's no way we can tell you how to do that.
We can tell you that the subject.toArray(new String[subject.size()]) assignment is redundant, since you throw the value away on the next line.
We can also tell you that the if (subjects != null) { test within the loop is redundant. If subjects was null , you'd have got an exception on the previous line trying to read its length property.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi both,
I find generic List is rather hard to use in java cos of the compiler can't recognize the type etc...from what I have read.
Thus, I have changed it to use Array but my poor Java skill is not helping cos the following code is also not working out. Hope you guys can point out where I have gone wrong.
Tks.
Controller :
subject m = new subject();
String[]subj = (request.getParameterValues("subject"));
for (int i = 0; i< subj.length; i++){
m.setSubj(subj);
i++;
mgr.insertSubject(subj);
SubjectDAOImpl:
public void insertSubject(String[] subj) throws MyDataException {
try {
subject = null;
openConnection();
String qry = INSERT_QRY1;
ps = connection.prepareStatement(qry);
ps.setObject(1, subject.getSubj());
ps.executeUpdate();
if (ps !=null )
closeConnection();
}
catch (SQLException e) {
e.printStackTrace();
public class subject {
public subject() {
super();
}
private String[] subj;
public String[] getSubj() {
return subj;
}
public void setSubj(String[] subj) {
this.subj = subj;
}
|
|
|
|
|
karengsh wrote: I find generic List is rather hard to use in java cos of the compiler can't recognize the type etc...from what I have read. I have no idea where you read that, but it was certainly not in the Java documentation: List (Java Platform SE 7 )[^].
|
|
|
|
|
Hello Richard,
I read it in stackoverflow. Unfortunately, I did not have the link now.
It says something erasure ....
Anyway, I have reworked my code as follows but I do not know why my insert for this arrayList is still not working
SubjectDAOImpl :
public class subjectDAOImpl implements subjectDAO {
private final String INSERT_QRY1 = "INSERT INTO project.subject subject VALUES (?)";
<pre>public void insertSubject(subject s) throws MyDataException {
try {
s = new subject();
ArrayList<String>subj;
openConnection();
String qry = INSERT_QRY1;
ps = connection.prepareStatement(qry);
int i =0;
subj = new ArrayList<String>();
while(i< subj.size()){
ps.setString(1,subj.get(i));
ps.addBatch();
i++;
ps.executeBatch();
if (ps !=null )
closeConnection();
}
}
catch (SQLException e) {
e.printStackTrace();
Controller :
<pre>
try{
subject m = new subject();
ArrayList<String>subj = new ArrayList<String>();
String[] sub = request.getParameterValues("subject");
for (String s : sub)
subj.add(s);
m.setSubj(subj);
mgr.insertSubject(m);
I used Eclipse to debug, the controller part seems correct. Unfortunately, I do not why Eclipse is not debugging on my sujectDAOImpl...Have been trying for hours....
There were alot of strange errors like :
RequestFacade.getParameterValue Source not found
WebappClassLoader(WebappClassLoaderBase).loadClass(String)
NonRegisteringDriver.class
but I am not able to get the debugger to debug on the serveral breakpoints I placed on the DAOImpl class.
Hope you can tell me what's wrong with my code.
Tks.
|
|
|
|
|
Your insertSubject method makes no sense. You are passed a subject reference which you immediately overwrite with a newly created object, which will be empty. You also create a new array (subj = new ArrayList<String>(); ) and then try to iterate its contents. As to the other errors, you will need to look at the code where they occur.
|
|
|
|
|
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
Scanner keyb = new Scanner(System.in);
System.out.println("Input:");
String in = keyb.nextLine();
System.out.println("Output:");
String out = keyb.nextLine();
int sum = 0;
long product = 1;
double count = 0;
ArrayList<Integer> med = new ArrayList<Integer>();
File file = new File(in);
Scanner f = new Scanner(file);
while ( f.hasNextInt() ) {
int num = f.nextInt();
sum += num;
product *= num;
med.add(num);
++count;
}
PrintWriter pw = new PrintWriter(out);
pw.println("Sum: " + sum);
pw.println("Product: " + product);
pw.println("Average: " + (sum/count));
Collections.sort(med);
int middle = med.length /2;
if (med.length % 2 == 1) {
pw.println("Median: " + med.get(middle));
} else {
pw.println("Median: " + (med.get(middle) + med.get(middle-1)) / 2);
}
}
}
modified 2-Jun-17 19:36pm.
|
|
|
|
|
Member 12877930 wrote: med.length
l think you are trying to get the size of the ArrayList, you can use
med.size which will give you the size of the list.
However, remember that Collection is an interface, thus not supposed to be instantiated. So you can use any of the implementation classes to sort.
|
|
|
|
|
|
There are key differences between an ArrayList and an Array. You are trying to find the length of an ArrayList, not an array. You are looking for med.size() not med.length
|
|
|
|
|
1) im trying this in jsp (working)-
${examques[0].question}
${examques[1].question}
2) i want like this-
int i=0;int j=2
${examques[i].question}
${examques[j].question}
IT IS NOT WORKING.......
3) Same method i want to apply in javascript with these variable ...
please help.......
|
|
|
|
|